react-testing

Guides writing and reviewing Vitest and Testing Library tests for React components and hooks.

1|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/TierOne-Studio/spa-velocity --skill react-testing-tierone-studio
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-testing
Source: https://github.com/TierOne-Studio/spa-velocity/tree/main/.ruler/skills/react-testing
Command: npx skills add https://github.com/TierOne-Studio/spa-velocity --skill react-testing-tierone-studio

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React test suites often drift into brittle patterns: test-id queries, fireEvent misuse, arbitrary setTimeout waits, and misplaced e2e tests. This Skill enforces consistent, accessible, low-flake testing practices for a React SPA using Vitest, Testing Library, and Playwright. ## Core Features & Use Cases - Layer selection rules: Decide upfront whether a behavior belongs in a unit test, component test, or Playwright e2e test, always choosing the lowest sufficient layer. - Testing Library hard rules: Enforce query priority (role > label > placeholder > text > testId), userEvent over fireEvent, async findBy queries, and boundary-only mocking. - Ready-made patterns: Provider-wrapped render helpers, renderHook with wrappers, toast and error-state assertions, and targeted axe-core accessibility checks. - Use Case: When writing a test for a new dialog component, apply the query priority rules, wrap the render with QueryClient and router providers, and assert on role-based queries instead of test IDs. ## Quick Start Ask the assistant to write a Vitest component test for a React form following the react-testing query priority and userEvent rules.

Frequently Asked Questions about react-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test React components with Testing Library and Vitest?

Render the component with the providers it needs (QueryClient, router, auth), then query by role or label and interact with userEvent. Use findBy queries for async UI updates and assert on visible text or ARIA attributes, not internal state.

What is the correct query priority in Testing Library?

Query priority is role first, then label, placeholder, text, and testId as a last resort. Role-based queries like getByRole('button', { name: 'Save' }) verify accessibility for free, while test IDs couple tests to implementation details.

Should I use userEvent or fireEvent in React tests?

Use userEvent. It simulates the real event cascade (focus, mousedown, mouseup, click), while fireEvent skips intermediate events and breaks tests covering focus or keyboard behavior.

When should I write a component test instead of an e2e test?

Choose the lowest layer that proves the requirement. Loading, empty, error, and form states are component tests; reserve Playwright e2e for cross-page workflows, route guards, auth flows, and real backend integration.

Why are my React tests flaky with async updates?

Flakiness usually comes from arbitrary setTimeout waits or missing async queries. Replace sleeps with findByRole or findByText, which retry until the element appears after mutations or loading states resolve.

How do I test custom React hooks that need providers?

Use renderHook with the wrapper option, supplying the providers the hook depends on, such as QueryClientProvider or an auth context. Assert on result.current rather than hook internals.