What does this skill do?

Testing React with RTL and MSW Skill provides comprehensive testing patterns for React components, custom hooks, and pages, prioritizing real-user behavior over implementation details. It includes network mocking with MSW, accessibility assertions with Axe, and guidelines for deciding when to scale up to E2E tools such as Playwright or Cypress.

React Component Tests
Write tests for new or legacy React components that don't yet have test coverage, using accessible queries and `userEvent`.
Migration from Enzyme
Migrate tests from Enzyme or class-based component patterns to React Testing Library without losing coverage.
Network Mocking with MSW
Configure `setupServer` with the `onUnhandledRequest` error handler so that any request that isn't mocked causes the test to fail.
Accessibility Statements
Run axe on each interactive component to detect missing labels, invalid ARIA, or missing alt attributes on images.

Usage examples

🧪 Form Test
Write a test for my UserForm component that sends data to /api/users using MSW and userEvent.
🪝 Hook test
Create tests for my custom hook `useCounter` using `renderHook` and `act()`.
🌐 MSW Settings
Configure MSW's setupServer with an onUnhandledRequest error handler and handlers for /api/users/:id.
♿ Accessibility Test
Add accessibility assertions using Axe to my UserCard component.

Features

Priority Accessible Appointments Recommend using `getByRole` and `getByLabelText` instead of `getByText` or `getByTestId`, avoiding `container.querySelector`.
Network Mocking with MSW Configure setupServer with the onUnhandledRequest error and override handlers for testing to simulate realistic HTTP requests.
Testing Hooks with renderHook Wrap state changes in `act()` and test only through the hook's public API.
Accessibility assertions with axe Detects missing labels, invalid ARIA attributes, and missing alt text in images before they go into production.
Layered Coverage Set goals: pure profit ≥90%, hooks ≥85%, components ≥80%, containers ≥70%, and a minimum number of smoke tests per page.

Frequently asked questions

Vitest for modern projects using Vite or Remix (faster, native ESM, Jest-compatible API). Jest for Next.js, CRA, or established repositories. Do not mix RTL+Vitest with Playwright CT without a clear separation.
userEvent simulates real browser sequences by calling userEvent.setup() once per test. fireEvent triggers a single synthetic event and does not reflect the full user interaction.
Yes. MSW is a dependency of the project. The skill guides you through configuring `setupServer`, `handlers`, and the `beforeAll`/`afterEach`/`afterAll` cycle, but the package must be installed in your repository.
Yes. They are broken with every style change and automatically approved during review. Reserve snapshots for pure data serialization functions, not for components.
React Testing with RTL and MSW — Component Testing with Claude AI

¿Prefieres escuchar el contenido? Genera la narración de audio con un clic.