React Testing
Behavior-focused React component, hook, and page tests. Detalhes em references/.
When to Activate
- Writing tests for React components, custom hooks, or pages
- Adding test coverage to legacy untested components
- Migrating from Enzyme or class-component-era patterns to React Testing Library
- Setting up Vitest or Jest for a new React project
- Mocking HTTP requests in tests
- Asserting accessibility violations
Core Principles
- Test behavior, not internals — visible output and side effects, never state/props/render counts
- Accessible queries first —
getByRole > semantic > getByTestId (last resort)
userEvent over fireEvent — setup() once per test, always await
- Async via
findBy/waitFor — never setTimeout + assertion
- MSW at the network layer —
onUnhandledRequest: "error", per-test server.use overrides
Example
test("submits the form", async () => {
const user = userEvent.setup();
const
render(<UserForm />);
await user.type(screen.getByLabelText("Email"), "user@example.com");
await user.click(screen.getByRole("button", { name: /save/i }));
expect(onSubmit).toHaveBeenCalledWith({ email: "user@example.com" });
});
References
references/queries-interaction.md — runner choice, query priority, userEvent, async patterns
references/network-providers.md — MSW setup/override, provider wrapping, hook testing
references/a11y-snapshots-e2e.md — axe, snapshot limits, Playwright/Cypress boundary, coverage, anti-patterns
references/tdd-commands-examples.md — TDD workflow, test commands, related, full examples
Checklist
1---2name: react-testing3description: "Use when writing or fixing tests for React components, hooks, or pages with Testing Library, Vitest/Jest, MSW, and axe. Triggers on \"react-testing\", \"react testing\", \"testing\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/react-testing6 origin: ECC7---89# React Testing1011Behavior-focused React component, hook, and page tests. Detalhes em `references/`.1213## When to Activate1415- Writing tests for React components, custom hooks, or pages16- Adding test coverage to legacy untested components17- Migrating from Enzyme or class-component-era patterns to React Testing Library18- Setting up Vitest or Jest for a new React project19- Mocking HTTP requests in tests20- Asserting accessibility violations2122## Core Principles23241. **Test behavior, not internals** — visible output and side effects, never state/props/render counts252. **Accessible queries first** — `getByRole` > semantic > `getByTestId` (last resort)263. **`userEvent` over `fireEvent`** — `setup()` once per test, always `await`274. **Async via `findBy`/`waitFor`** — never `setTimeout` + assertion285. **MSW at the network layer** — `onUnhandledRequest: "error"`, per-test `server.use` overrides2930## Example3132```tsx33test("submits the form", async () => {34 const user = userEvent.setup();35 const onSubmit = vi.fn();36 render(<UserForm onSubmit={onSubmit} />);3738 await user.type(screen.getByLabelText("Email"), "user@example.com");39 await user.click(screen.getByRole("button", { name: /save/i }));4041 expect(onSubmit).toHaveBeenCalledWith({ email: "user@example.com" });42});43```4445## References4647- `references/queries-interaction.md` — runner choice, query priority, `userEvent`, async patterns48- `references/network-providers.md` — MSW setup/override, provider wrapping, hook testing49- `references/a11y-snapshots-e2e.md` — axe, snapshot limits, Playwright/Cypress boundary, coverage, anti-patterns50- `references/tdd-commands-examples.md` — TDD workflow, test commands, related, full examples5152## Checklist5354- [ ] Queries by role/label, `getByTestId` only as escape hatch55- [ ] Providers wrapped once in `test-utils`; QueryClient `retry: false`56- [ ] MSW `onUnhandledRequest: "error"`; no unmocked network passes silently57- [ ] axe green on interactive components; no DOM snapshots58- [ ] RTL for logic, Playwright CT for browser APIs, E2E for multi-page flows