The Testing Pyramid: Unit, Integration, E2E
What you'll learn: How the testing pyramid structure guides you to write more unit tests, fewer integration tests, and minimal E2E tests for optimal coverage and speed.
The Pyramid Concept
Imagine a food pyramid where the foundation is what you eat most, and the top is what you consume sparingly. The testing pyramid works the same way:
- Bottom (Wide): Unit tests — many, fast, isolated tests of individual functions and components
- Middle: Integration tests — moderate number of tests checking how components work together
- Top (Narrow): E2E tests — few, slow, comprehensive tests running the entire application
Why This Structure?
Speed and cost increase as you move up:
- Unit tests run in milliseconds, catch bugs instantly during development
- Integration tests (like React Testing Library component tests with MSW) take seconds, verify realistic interactions
- E2E tests (Playwright, Cypress) take minutes, require full browser setup, but test real user flows
The E2E paradox: E2E tests provide the most confidence that your app actually works, but they're expensive to write, slow to run, and brittle when UI changes. If you only wrote E2E tests, your test suite would take hours and frustrate developers.
The Optimal Balance
Write many unit tests for your utility functions, hooks, and pure logic. Write some integration tests for critical user flows within components. Write few E2E tests only for your most critical paths—like the signup flow or checkout process.
This gives you the best coverage-to-cost ratio: fast feedback during development, confidence in component integration, and verification that the real app works.
Key Takeaway: The testing pyramid recommends fewer E2E tests at the top because they're slow and expensive—rely on a broad base of fast unit and integration tests for most coverage, using E2E only for critical end-to-end user journeys.