Integration Testing Multi-Page User Journeys
Integration Testing Multi-Page User Journeys
While unit tests verify isolated functions or components in a vacuum, integration tests verify how multiple components, routers, state providers, and mocked API responses collaborate across realistic user journeys. Using React Testing Library (RTL) and Vitest, you can test multi-step workflows like user login, product checkout, and multi-tab forms with high confidence.
1. Guiding Philosophy: Test User Behavior, Not Implementation
- Avoid: Testing internal component state (
wrapper.state()), testing internal hook variables, querying CSS class selectors. - Prefer: Interacting via accessible queries (
getByRole,getByLabelText,getByText), clicking buttons via@testing-library/user-event, asserting visible changes.
2. Setting Up an Enterprise Test Harness
Because enterprise components rely on Context providers (Router, Auth, Theme, QueryClient), build a reusable test harness renderWithProviders:
3. Multi-Step Checkout Journey Integration Test
Let's test an end-to-end user journey:
- 1User enters address in Shipping Form.
- 2User clicks "Continue to Payment".
- 3User selects Credit Card and clicks "Place Order".
- 4User sees order confirmation screen.
4. Query Priority Guidelines
When selecting elements in React Testing Library, follow this accessibility-first priority order:
- 1
getByRole(e.g.getByRole('button', { name: /submit/i })) - Mirrors how screen readers access the DOM. - 2
getByLabelText- Best for form inputs with linked<label>tags. - 3
getByPlaceholderText- Fallback if no label exists. - 4
getByText- Best for non-interactive text elements (paragraphs, headings). - 5
getByTestId- Last resort when no accessible role or label is feasible.
Practice Quiz
Mocking Network Requests with Mock Service Worker (MSW)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.