react-testing

Write behavior-focused React component and hook tests with Testing Library, Vitest, and MSW.

1|Updated Aug 31, 2026
One-click install
npx skills add https://github.com/gagandeepgill/puzzle-game --skill react-testing-gagandeepgill
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: react-testing
Source: https://github.com/gagandeepgill/puzzle-game/tree/main/.claude/skills/react-testing
Command: npx skills add https://github.com/gagandeepgill/puzzle-game --skill react-testing-gagandeepgill

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React tests often break on refactors because they assert on implementation details like state, props, and render counts instead of user-visible behavior. This Skill provides patterns for writing stable component, hook, and accessibility tests, plus guidance on when JSDOM-based tests are insufficient and a real browser is required. ## Core Features & Use Cases - Behavior-Focused Component Tests: Query by role and label, interact via userEvent, and assert on visible output rather than internals. - Network Mocking with MSW: Intercept HTTP at the network layer so components and hooks behave exactly as in production, with per-test handler overrides for error states. - Hook, Accessibility, and Async Testing: Test custom hooks with renderHook, run axe violation checks, and handle async UI with findBy and waitFor instead of flaky timeouts. - Use Case: When adding tests to a legacy form component, wrap it in the same providers as production, mock its API calls with MSW, simulate typing and submission with userEvent, and assert the success message appears. ## Quick Start Write a React Testing Library test for my UserForm component that mocks the save API with MSW and asserts the success message appears after submission.

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 React Testing Library?▼

Render the component with the same providers it uses in production, query elements by role or label, interact with userEvent, and assert on visible output. Avoid inspecting state, props, or render counts since those are implementation details.

Vitest vs Jest for React testing, which should I use?▼

Vitest suits Vite, Remix, and modern setups with faster runs and native ESM, while Jest remains the default for Next.js, CRA, and established repos. Pick one runner rather than mixing both in the same project.

How do I mock API requests in React component tests?▼

Use Mock Service Worker to intercept requests at the network layer so components and fetch libraries behave as in production. Configure onUnhandledRequest to error so unmocked requests fail loudly, and override handlers per test for error states.

When should I use Playwright instead of React Testing Library?▼

Use Playwright when tests need real layout, CSS transitions, scrolling, drag-and-drop, iframes, or downloads, since JSDOM cannot handle these. Keep hooks, presentational components, and form logic in RTL, and reserve Playwright for layout-sensitive components and full user flows.

Why are my React hook tests flaky with React Query?▼

Flakiness occurs when the QueryClient is created inside the wrapper closure, resetting cache state on every render. Instantiate the QueryClient once per test outside the wrapper and disable retries in its default options.

Are snapshot tests a good way to test React components?▼

Snapshot tests of rendered output break on every styling change and test DOM structure rather than behavior, so they are discouraged. Reserve snapshots for pure serialization functions and use Playwright screenshots or visual regression tools for components.