Testing everything at the UI level is slow and brittle, but testing nothing is risky. A suite built in layers works better, with fast tests catching logic bugs and slower integration and visual tests covering what the fast tests can’t. This guide covers how to build a test suite you can trust without it taking too long to run.
The Testing Layers
Each layer catches different kinds of bugs and runs at a different speed. The testing pyramid still applies, so you want lots of fast tests at the bottom and fewer slow tests at the top.
- Component tests: Fast, focused tests for individual UI components, using Component Testing.
- Logic tests: Even faster tests for reducers and hooks, using Stateful Logic Testing. They test pure logic, so they don’t need a DOM or any rendering.
- Integration tests: Slower tests, using Integration Testing, that check several components work together, including routing and data fetching.
- Visual tests: Screenshot comparisons, using Visual Regression, that catch UI changes you didn’t intend to make.
- Accessibility tests: Automated checks plus manual screen reader testing, using Accessibility Testing.
Create Maintainable Test Data
If you copy large JSON fixtures into your tests, keeping them up to date can become enough work that your team stops wanting to write tests at all.
- Use test builders: You can create Test Data Builder functions that generate valid test data with defaults. Then getting a test user is as simple as calling
buildUser(), orbuildUser({ role: 'admin' })when you need a specific role. - Keep builders close: Builders are easiest to maintain when you keep them next to the code they create data for. If a data model changes, the tests break right away and it’s obvious why, so you can see which updates you need to make.
- Provide smart defaults: A builder should work without any configuration, but still let you override any property. That avoids common problems such as undefined property errors, and it keeps your tests short.
Write Effective Component Tests
Component tests check how a single component behaves, and they should stay fast and focused. Most of them run in a simulated DOM such as jsdom, so they don’t need to launch a browser.
- Mock minimally: It’s best to mock only what you have to, so real context providers and hooks still run and other dependencies stay real where possible. If you mock too much, your tests might pass while the real application is broken.
- Test user-facing behavior: Good component tests check what users see and do, such as whether clicking a button triggers the right action, or whether form validation works. It’s best to avoid testing implementation details such as state variable names, because those tests break when you refactor.
- Keep tests fast: Component test suites should run in seconds. If they take minutes, it’s worth finding out why, because developers run fast tests often and skip slow ones.
Test Complete User Journeys
Integration tests check that components work together. They’re slower, but they catch problems that component tests miss.
- Test critical paths: Integration Testing works best for essential flows such as login and checkout. You can mock API calls with MSW and still test real routing and real state management.
- Be selective: One integration test per critical user journey is usually enough. More than that tends to make the suite slow and flaky.
- Verify analytics: It’s worth testing that your tracking events are sent correctly. If your analytics are broken, your team might be making decisions with missing data, and often doesn’t notice until someone asks why some metrics are empty.
Protect Visual Design and Accessibility
Component and integration tests check that the code behaves correctly. Visual and accessibility tests check what people see, and whether everyone can use the interface.
- Snapshot key screens: You can run Visual Regression tests on design system components and critical pages, in both light and dark themes and at different screen sizes, so you catch accidental CSS changes early.
- Test accessibility automatically: With Accessibility Testing in place, you can check ARIA attributes and color contrast automatically. Automated tools catch the common issues, and manual screen reader testing catches the problems that only a person would notice, so you need both.
Maintain Your Test Suite
A test suite gets worse if your team doesn’t maintain it, so it’s worth looking after it the same way you look after production code.
- Track layered coverage: An overall coverage percentage doesn’t tell you much. It’s more useful to track which critical flows have component tests, integration tests, visual tests, and accessibility tests. If a critical flow is missing one of those layers, that’s the first thing to fix.
- Fix flaky tests immediately: A test that sometimes passes and sometimes fails is worse than no test at all, because developers learn to ignore its failures. I’d fix a flaky test the same day it appears, or delete it.
- Match tests to workflow: Fast component and logic tests can run on every commit, and the slower integration and visual tests can run on each pull request. Full accessibility audits can wait until just before a release.
Remember: A test suite only helps if your team actually runs it, and teams tend to start disabling tests once they get slow or flaky.