Frontend Testing with Vitest and Testing Library
8 steps · about 3 h 25 min · Intermediate
- Getting started with Vitest: configuration and test execution (guide, Vitest Documentation, 25 min)
Configure Vitest test runners, write first describe and test blocks, and run tests in watch mode.
- Assertions and matchers in Vitest (reference, Vitest Documentation, 25 min)
Use expect matchers like toBe, toEqual, toContain, and toThrow to verify expected outcomes.
- Guiding principles of Testing Library (guide, Testing Library, 20 min)
Test software the way users use it: query by accessible roles and text rather than internal details.
- DOM queries: getBy, findBy, and queryBy priorities (reference, Testing Library, 25 min)
Prioritize accessible queries: getByRole, getByLabelText, and getByText for robust selectors.
- Testing React components with React Testing Library (tutorial, Testing Library, 30 min)
Render React components into jsdom, query rendered elements, and assert visual state.
- Simulating realistic user interactions with user-event (guide, Testing Library, 25 min)
Simulate typing, clicking, selecting options, and keyboard tabs with async userEvent APIs.
- Mocking functions, modules, and API calls with vi.fn and vi.mock (guide, Vitest Documentation, 30 min)
Spy on callbacks and mock network fetch responses to isolate components during tests.
- Testing asynchronous UI updates with waitFor and findBy (guide, Testing Library, 25 min)
Wait for elements to appear or disappear cleanly without arbitrary sleep timers.