Use a testing pyramid or trophy: static checks (TypeScript, ESLint including the hooks rules) at the base, unit tests for pure logic and hooks, component/integration tests with React Testing Library (RTL) as the bulk, and a smaller set of end-to-end tests (Playwright or Cypress) for critical user journeys. Test behaviour as a user perceives it: query by role and label, interact with user-event, and assert on visible output. Avoid testing implementation details like state values or which hooks ran. Mock the network at the boundary with MSW.
| Layer | Tools | What to test |
|---|---|---|
| Static | TypeScript, ESLint (react-hooks, jsx-a11y) | Types, hook rules, obvious a11y problems |
| Unit | Vitest or Jest | Reducers, utilities, formatters, custom hooks (renderHook) |
| Component / integration | RTL + user-event + MSW | Components wired together with real children and a mocked network |
| E2E | Playwright, Cypress | Login, checkout, critical flows in a real browser |
| Visual / a11y | Storybook + Chromatic, axe | Regressions in appearance, WCAG violations |
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { server } from './test/server';
import TodoApp from './TodoApp';
test('adds a todo and shows it in the list', async () => {
server.use(
http.post('/api/todos', async ({ request }) => {
const { text } = await request.json();
return HttpResponse.json({ id: '1', text }, { status: 201 });
})
);
const user = userEvent.setup();
render(<TodoApp />);
await user.type(screen.getByRole('textbox', { name: /new todo/i }), 'Write tests');
await user.click(screen.getByRole('button', { name: /add/i }));
expect(await screen.findByRole('listitem')).toHaveTextContent('Write tests');
});
Why this is robust:
user-event simulates realistic sequences (focus, keydown, input, click) rather than raw fireEvent.findBy* waits for async UI, so you don't need manual act() or sleeps.renderHook(() => useCounter()) from RTL. Better still, test them through a small component that uses them.wrapper providing real providers (router, query client, theme) via a custom render helper.act for you. Warnings about act usually mean an un-awaited update. Use findBy/waitFor.use: render inside a <Suspense> boundary in tests and await the resolved content.user-event, and MSW instead of mocking fetch.Spotted a mistake or a rights concern? Report an issue