Agent-skills-standard react-testing
Test React components with RTL and Jest/Vitest. Use when writing React component tests with React Testing Library, Jest, or Vitest. (triggers: **/*.test.tsx, **/*.spec.tsx, render, screen, userEvent, expect)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/react/react-testing" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-react-testing && rm -rf "$T"
manifest:
skills/react/react-testing/SKILL.mdsource content
React Testing
Priority: P2 (MAINTENANCE)
Implementation Guidelines
- Standards: Use React Testing Library (RTL) with Vitest or Jest. Follow Arrange-Act-Assert (AAA) pattern.
- Selection: Prefer
/getByRole
to test accessibility. UsefindByRole
only as fallback for complex UI.data-testid - Interactions: Use
(async) instead ofuserEvent
to better simulate browser events (e.g.,fireEvent
).await user.click(element) - Asynchrony: Use
for elements that appear later. Useawait screen.findBy*
for complex non-element updates.waitFor(() => ...) - Networking: Mock all API calls with Mock Service Worker (MSW). Never call real APIs in unit/integration tests.
- Architecture: Test behavior, not implementation. Avoid checking internal
orstate
. Ensure 100% of P0 flows covered.props - Mocks: Mock expensive third-party libraries (e.g.,
,framer-motion
) or heavy assets to speed up tests.react-router - Visuals: Use Snapshot testing sparingly for stable, small UI components. Manual a11y checks with
.jest-axe
Anti-Patterns
- No Shallow Rendering: Render full tree.
- No Testing Implementation Details: Don't check
.component.state - No Wait: Use
, avoidfindBy
if possible.waitFor
References
See references/REFERENCE.md for MSW API mocking, Context testing, form testing, and React Router patterns.
Code
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(); });