金色时刻 · 15:48
让好资源,触手可及
AGENT SKILL

testing

testing 是面向测试与安全的 Agent Skill,来自 HoangNguyen0403/agent-skills-standard。查看技能说明、操作步骤与使用约定,并通过原项目了解详细用法。

Testing strategies with RTL and Jest/Vitest.

SKILL.md

下载 MD ↓
nameReact Testing
descriptionTesting strategies with RTL and Jest/Vitest.
metadata{"labels": ["react", "testing", "jest", "vitest"], "triggers": {"files": ["**/*.test.tsx", "**/*.spec.tsx"], "keywords": ["render", "screen", "userEvent", "expect"]}}

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

test('submits form', async () => {
  const user = userEvent.setup();
  render(<LoginForm />);

  await user.type(screen.getByLabelText(/email/i), '[email protected]');
  await user.click(screen.getByRole('button', { name: /login/i }));

  expect(await screen.findByText(/welcome/i)).toBeInTheDocument();
});

来源:HoangNguyen0403/agent-skills-standard · MIT 许可 · 许可原文 · MD 为来源页保存的历史版本