react-component-testing

Write React component tests with Testing Library, MSW network mocking, and vitest-axe accessibility assertions.

1|1|Updated May 24, 2026
One-click install
npx skills add https://github.com/bm629/agent-skills --skill react-component-testing-bm629
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-component-testing
Source: https://github.com/bm629/agent-skills/tree/main/skills/react-component-testing
Command: npx skills add https://github.com/bm629/agent-skills --skill react-component-testing-bm629

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Component tests for a Vite + React + TypeScript SPA often mock the wrong layer, race on async interactions, or silently skip accessibility checks. This Skill sets up and authors a coherent component-test layer — React Testing Library for user-centric rendering, Mock Service Worker v2 for network-boundary mocking, and vitest-axe for runtime accessibility assertions — so tests exercise the real client and catch contract drift. ## Core Features & Use Cases - Environment setup: Configures the jsdom Vitest environment (never happy-dom, which breaks axe), @vitejs/plugin-react, and setupFiles wiring jest-dom matchers, RTL cleanup, the MSW server lifecycle, and the vitest-axe matcher. - Network-boundary mocking: Intercepts HTTP with MSW v2 setupServer so a generated @hey-api/openapi-ts client and its serialization run for real, with per-test handler overrides and request-body capture. - User-realistic interactions: Enforces accessible-query priority (getByRole first), always-awaited user-event v14 calls, findBy*/waitFor async handling, and a per-test TanStack Query QueryClientProvider with retry: false. - Use Case: Testing a signup form — fill fields via user-event, capture the real request body in an MSW handler, assert the success state renders, and assert toHaveNoViolations on the shipped state. ## Quick Start Use react-component-testing to set up the jsdom test environment and write a component test for my SignupForm that mocks the API with MSW and checks accessibility.

Frequently Asked Questions about react-component-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 and MSW?

Render the component with a custom render that wraps providers, drive it with awaited user-event calls, and intercept HTTP requests with MSW's setupServer so the real client and serialization run. Assert visible UI state and the request body captured by the MSW handler.

Should I mock the API client with vi.mock or use MSW?

Use MSW at the network boundary, not vi.mock on the client module. Module mocking skips the generated client and serialization entirely, so contract drift like renamed fields never surfaces; MSW intercepts real HTTP requests so those failures appear.

Why does vitest-axe not work with happy-dom?

Happy-dom has a documented Node.prototype.isConnected behavior that breaks axe-core's DOM traversal, so vitest-axe assertions malfunction under it. Run any test using toHaveNoViolations under the jsdom environment instead.

How do I test TanStack Query loading and error states?

Create a fresh QueryClient per render with retry set to false so error states resolve immediately, then vary the MSW response per test with server.use to drive loading, empty, success, and error states. Assert what the user sees with findBy queries.

Why are my user-event tests flaky or passing incorrectly?

In user-event v14 every interaction returns a Promise, so a missing await lets assertions run before the interaction completes. Always call userEvent.setup() once per test and await every click, type, and keyboard interaction.

Does passing axe accessibility tests mean my component is fully accessible?

No. Automated axe-core detects only a subset of WCAG issues, roughly the mechanical checks like missing labels and contrast. A passing toHaveNoViolations result is necessary but must be complemented by manual keyboard and assistive-technology review.