Test React components and user flows with Testing Library
11 steps · about 3 h 55 min · Intermediate
- React Testing Library (doc, Testing Library (official docs), 10 min)
Start with the official install steps and the idea that tests should resemble how users use the app.
- React Testing Library Tutorial – How to Write Unit Tests for React Apps (tutorial, freeCodeCamp, 40 min)
A hands-on setup with Vitest or Jest and jest-dom, ending with your first passing tests.
- React Testing Library Tutorial (with Jest & Vitest) (tutorial, Robin Wieruch, 45 min)
Goes further than the first tutorial: rendering, queries, user events and testing async data fetching in one project.
- About Queries (doc, Testing Library (official docs), 15 min)
Learn the query priority, with getByRole first, before you write many tests.
- Introduction (user-event) (doc, Testing Library (official docs), 10 min)
Learn the userEvent.setup() pattern for clicking and typing like a real user.
- Utility APIs (user-event) (reference, Testing Library (official docs), 15 min)
Practise type, clear, selectOptions and upload, the building blocks of form flows.
- Example (doc, Testing Library (official docs), 15 min)
A full arrange-act-assert example of a user flow that loads data and waits with findBy.
- Common mistakes with React Testing Library (blog, Kent C. Dodds, 25 min)
Once you have written some tests, this fixes habits such as misusing waitFor, skipping user-event and over-using test IDs.
- Common Testing Mistakes (blog, Kent C. Dodds, 20 min)
Explains why testing implementation details makes tests brittle and how to keep them isolated.
- Write fewer, longer tests (blog, Kent C. Dodds, 10 min)
Shows how to structure a whole user flow in one test and avoid act warnings.
- MSW Testing Guide: Mock Service Worker with Jest, Vitest & React Testing Library (article, HelpMeTest, 30 min)
Last step: mock the network with MSW 2.x so full user flows run without a real API.