Frontend Testing with Vitest and Testing Library

8 steps · about 3 h 25 min · Intermediate

  1. 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.

  2. Assertions and matchers in Vitest (reference, Vitest Documentation, 25 min)

    Use expect matchers like toBe, toEqual, toContain, and toThrow to verify expected outcomes.

  3. 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.

  4. DOM queries: getBy, findBy, and queryBy priorities (reference, Testing Library, 25 min)

    Prioritize accessible queries: getByRole, getByLabelText, and getByText for robust selectors.

  5. Testing React components with React Testing Library (tutorial, Testing Library, 30 min)

    Render React components into jsdom, query rendered elements, and assert visual state.

  6. Simulating realistic user interactions with user-event (guide, Testing Library, 25 min)

    Simulate typing, clicking, selecting options, and keyboard tabs with async userEvent APIs.

  7. 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.

  8. 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.