react-testing

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

Updated May 7, 2026
One-click install
npx skills add https://github.com/mirzadham/trainingroombookingsystem2 --skill react-testing-mirzadham
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: react-testing
Source: https://github.com/mirzadham/trainingroombookingsystem2/tree/main/.pi/skills/react-testing
Command: npx skills add https://github.com/mirzadham/trainingroombookingsystem2 --skill react-testing-mirzadham

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React tests often break on refactors because they assert implementation details, mock too much, or flake on async behavior. This Skill provides opinionated patterns for writing stable, user-centric component and hook tests that survive code changes. ## Core Features & Use Cases - Behavior-First Testing Patterns: Query priority guidance (getByRole over getByTestId), userEvent interactions, and async matchers like findBy and waitFor to eliminate flaky setTimeout assertions. - Network Mocking with MSW: Network-level request interception with per-test handler overrides, so components and fetch logic behave exactly as in production. - Accessibility & Coverage Standards: axe-based violation assertions in component tests plus concrete coverage thresholds per layer (utilities, hooks, components, pages). - Use Case: You inherit a legacy React codebase with Enzyme-era tests. Use this Skill to migrate to React Testing Library, wrap tests in a shared renderWithProviders helper, mock API calls with MSW, and decide which flows belong in Playwright E2E instead. ## Quick Start Write a React Testing Library test for my UserForm component that fills the email field, submits, mocks the POST request with MSW, and asserts the success message appears.

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 its production providers, interact via accessible queries like getByRole and userEvent, and assert visible output or observable side effects. Avoid inspecting component 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 ESM setups with faster execution and a Jest-compatible API. Jest remains the default for Next.js, CRA, and established repositories. Pick one runner rather than mixing both in the same repo.

How do I mock API requests in React tests with MSW?▼

Mock Service Worker intercepts requests at the network layer using setupServer with http handlers, so components and fetch logic run as in production. Configure onUnhandledRequest to error and override handlers per test with server.use for error-state scenarios.

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

Use Playwright Component Testing or E2E when JSDOM cannot handle the scenario: real layout, CSS transitions, scrolling, drag-and-drop, iframes, downloads, or multi-page user flows. Hooks, presentational components, and form logic stay in RTL.

Why are my React tests flaky with async updates?▼

Flakiness usually comes from setTimeout-based assertions or unwrapped state updates. Use findBy queries for elements appearing after async work, waitFor for side effects, and act around state-changing hook calls instead of arbitrary delays.

Should I use snapshot tests for React components?▼

Avoid DOM snapshots for components because they break on styling changes and test structure rather than behavior. Snapshots are acceptable for pure serialization functions or generated config; use Playwright screenshots or Chromatic for visual regression.