/react-testing-techniques:react-testingfrom react-testing-techniques
Guides testing React components with Vitest, React Testing Library, and MSW. Covers query selection, userEvent patterns, async testing, and Mantine integration.
Ships in react-testing-techniques. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Best practices for testing React components with Vitest, React Testing Library (RTL), and Mock Service Worker (MSW).
Write tests that interact with components the way users do. Avoid testing implementation details.
userEvent, not fireEventCombine multiple components and use MSW for API mocking at the network level. Avoid mocking component internals, hooks, or services unless absolutely necessary.
Choose the right query based on the scenario:
| Scenario | Query | Why |
|---|---|---|
| Element exists synchronously | getBy* | Throws if not found - fails fast |
| Element appears after async operation | findBy* | Waits with retry, use with await |
| Assert element does NOT exist | queryBy* | Returns null instead of throwing |
| Multiple elements expected | *AllBy* variants | Returns array of matches |
Priority order for queries:
getByRole - most accessible, preferredgetByLabelText - for form fieldsgetByPlaceholderText - for inputsgetByText - for non-interactive contentgetByTestId - last resort onlyAlways use userEvent.setup() before interactions:
import { render, screen } from '../test/test-utils';
import userEvent from '@testing-library/user-event';
test('submits form correctly', async () => {
const user = userEvent.setup();
render(<MyForm />);
await user.type(screen.getByRole('textbox', { name: /email/i }), '[email protected]');
await user.click(screen.getByRole('button', { name: /submit/i }));
expect(await screen.findByText(/success/i)).toBeInTheDocument();
});
Never use fireEvent - it dispatches raw DOM events without realistic user interaction simulation.
findBy* for Elements That Appear Async// Element appears after data loads
expect(await screen.findByText('John Doe')).toBeInTheDocument();
waitFor for Assertions Only// Wait for a side effect or state change
await waitFor(() => expect(handleSubmit).toHaveBeenCalledTimes(1));
waitForElementToBeRemoved for Disappearing Elements// Wait for loading spinner to disappear
await waitForElementToBeRemoved(() => screen.getByText('Loading...'));
Use Mock Service Worker for API mocking. Always use v2 syntax:
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer(
http.get('/api/users', () => {
return HttpResponse.json([{ id: 1, name: 'John' }]);
})
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
Override handlers per test:
test('handles error state', async () => {
server.use(
http.get('/api/users', () => {
return HttpResponse.json({ error: 'Not found' }, { status: 404 });
})
);
// Test error handling...
});
Use Vitest APIs, not Jest:
| Operation | Vitest | Not Jest |
|---|---|---|
| Mock function | vi.fn() | |
| Mock module | vi.mock() | |
| Spy on method | vi.spyOn() | |
| Stub env var | vi.stubEnv() | |
| Reset modules | vi.resetModules() | |
| Restore mocks | vi.restoreAllMocks() |
Environment variable testing:
import { afterEach, vi } from 'vitest';
afterEach(() => {
vi.unstubAllEnvs();
vi.resetModules();
});
test('handles missing env var', async () => {
vi.stubEnv('API_KEY', '');
const { config } = await import('./config');
expect(() => config.apiKey).toThrow();
});
Create a custom render with all providers. Read references/test-utils-setup.md for complete setup with:
env="test")Except for trivial components (<30 lines of output). Snapshots encode structure, not intent.
Use MSW for APIs instead of mocking fetch/axios. Mock only at system boundaries.
Consult these for detailed patterns:
references/test-utils-setup.md - Complete custom render setup with providersreferences/query-selection.md - Detailed query type decision guidereferences/vitest-patterns.md - Vitest mocking, spying, and stubbingreferences/msw-patterns.md - MSW v2 handlers, overrides, error simulationreferences/async-testing.md - waitFor, findBy, timing patternsreferences/tanstack-testing.md - TanStack Query and Router testingreferences/user-interactions.md - userEvent patterns and edge casesreferences/mantine-testing.md - Mantine component testing (modals, forms, select)references/async-testing.md (opens in a new tab)references/mantine-testing.md (opens in a new tab)references/msw-patterns.md (opens in a new tab)references/query-selection.md (opens in a new tab)references/tanstack-testing.md (opens in a new tab)references/test-utils-setup.md (opens in a new tab)references/user-interactions.md (opens in a new tab)references/vitest-patterns.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
npx claudepluginhub mrclrchtr/skills --plugin react-testing-techniques