首页/ AI 技能 / @skilllib/skill-2117939930272417243
Skill站外收录 @skilllib/skill-2117939930272417243

testing

提供基于用户行为的 React 组件测试能力,通过模拟真实交互验证功能逻辑与界面反馈,强调可访问性支持和异步操作处理,避免依赖内部实现细节,适用于保障前端功能稳定性和可维护性。

0.0 (0) 17 次安装 原作者 编程导航
收录于 2026-01-22
测试与安全 AI 技能

Skills.MD

# React Testing ## **Priority: P2 (MAINTENANCE)** Reliable tests focusing on user behavior. ## Implementation Guidelines - **Tooling**: React Testing Library + Vitest. - **Philosophy**: Test behavior, not implementation (State/Internal vars). - **Queries**: `getByRole` > `getByText` > `getByTestId`. - **Events**: Use `userEvent` (async) over `fireEvent`. - **Async**: `await screen.findBy*` for async updates. - **Mocks**: MSW for network. Mock heavy 3rd-party libs. - **Accessibility**: Testing Lib implicitly tests a11y roles. ## Anti-Patterns - **No Shallow Rendering**: Render full tree. - **No Testing Implementation Details**: Don't check `component.state`. - **No Wait**: Use `findBy`, avoid `waitFor` if possible. ## Code ```tsx test('submits form', async () => { const user = userEvent.setup(); render(<LoginForm />); await user.type(screen.getByLabelText(/email/i), 'test@test.com'); await user.click(screen.getByRole('button', { name: /login/i })); expect(await screen.findByText(/welcome/i)).toBeInTheDocument(); }); ```

评论 0

还没有评论,来抢沙发。