Test React components through what users see and do: accessible queries, user-event, network-level mocking with MSW, and focused integration tests.
.cursor/rules/react-testing-strategies.mdc. Downloaded files need to be placed in that repository path.--- description: "Test React components through what users see and do: accessible queries, user-event, network-level mocking with MSW, and focused integration tests." globs: "**/*" alwaysApply: false --- Use these guidelines when writing or reviewing tests for React components. ## What to assert - Test behaviour a user or caller can observe: rendered text and roles, form behaviour, callbacks fired, requests made. Do not assert on internal state, hook calls or private helpers. - For each component, cover the loading, empty and error states and at least one edge case from its props or API contract, not only the happy path. ## How to query and interact - Use React Testing Library queries in this priority order: `getByRole`, `getByLabelText`, `getByPlaceholderText`, `getByText`, `getByDisplayValue`, then `getByAltText`, `getByTitle`, and `getByTestId` only as a last resort. - Drive interactions with `@testing-library/user-event`: create `const user = userEvent.setup()` before rendering and `await` each interaction. Use `findBy*` queries for content that appears asynchronously. ## Mocking - Mock at the network boundary: use MSW request handlers so components run their real fetching code against controlled responses. Override handlers per test for error and empty cases. - Mock modules only for things that cannot run in the test environment (router, analytics SDK, feature-flag client). Reset mocks between tests. ## Accessibility - Run automated axe checks (for example jest-axe or vitest-axe) on key screens. Automated checks catch only part of accessibility problems, so keep role/label-based queries and manual keyboard checks as well. ## Scope of tests - Write integration tests that render a feature with its real providers for critical flows (sign-in, checkout, form submission). Use browser end-to-end tests (Playwright, Cypress) only for flows that depend on real routing, authentication or browser APIs. - Use snapshot tests only for small, stable output where a diff is easy to review, and never as the only assertion in a test. - Use the runner the project already has (Vitest or Jest); do not migrate runners as part of a feature change.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
testing
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A SearchUsers component calls /api/users?q= and shows results, an empty message and an error banner. Existing tests use getByTestId and mock the component's own fetchUsers module.
Apply the React Testing Strategies rule: rewrite SearchUsers.test.tsx. The project uses Vitest, React Testing Library, user-event and MSW (handlers in src/mocks/handlers.ts).
Tests that type into the search box found by role/label with user-event, await results via findByRole, use MSW handlers (overridden per test) for a results response, an empty response and a 500, assert the empty and error messages, and contain no getByTestId unless no accessible query exists.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/react-testing-strategies.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
--- description: "React Testing Strategies project conventions" globs: "**/*" alwaysApply: false --- Use these guidelines when writing or reviewing tests for React components. ## What to assert - Test behaviour a user or caller can observe: rendered text and roles, form behaviour, callbacks fired, requests made. Do not assert on internal state, hook calls or private helpers. - For each component, cover the loading, empty and error states and at least one edge case from its props or API contract, not only the happy path. ## How to query and interact - Use React Testing Library queries in this priority order: `getByRole`, `getByLabelText`, `getByPlaceholderText`, `getByText`, `getByDisplayValue`, then `getByAltText`, `getByTitle`, and `getByTestId` only as a last resort. - Drive interactions with `@testing-library/user-event`: create `const user = userEvent.setup()` before rendering and `await` each interaction. Use `findBy*` queries for content that appears asynchronously. ## Mocking - Mock at the network boundary: use MSW request handlers so components run their real fetching code against controlled responses. Override handlers per test for error and empty cases. - Mock modules only for things that cannot run in the test environment (router, analytics SDK, feature-flag client). Reset mocks between tests. ## Accessibility - Run automated axe checks (for example jest-axe or vitest-axe) on key screens. Automated checks catch only part of accessibility problems, so keep role/label-based queries and manual keyboard checks as well. ## Scope of tests - Write integration tests that render a feature with its real providers for critical flows (sign-in, checkout, form submission). Use browser end-to-end tests (Playwright, Cypress) only for flows that depend on real routing, authentication or browser APIs. - Use snapshot tests only for small, stable output where a diff is easy to review, and never as the only assertion in a test. - Use the runner the project already has (Vitest or Jest); do not migrate runners as part of a feature change.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.