The Testing Pyramid
The classic testing pyramid — many unit tests, fewer integration tests, even fewer E2E tests — still holds in 2026. Unit tests are fast and isolated. Integration tests verify that components work together. E2E tests verify that user workflows work end-to-end in a real browser. The trap is inverting the pyramid: heavy E2E suites that are slow, flaky, and hard to debug.
Unit Testing with Vitest
Vitest has largely replaced Jest in the Vite/Next.js ecosystem thanks to its speed and native ESM support. Test pure functions with straightforward input/output assertions. For React components, test behavior not implementation — what the user sees and can interact with, not which internal functions were called.
Integration Testing
Integration tests hit real databases and real HTTP handlers. Use a test database that's seeded before each test run and torn down after. For Next.js API routes, use supertest or the built-in fetch with your test server. Test the full request/response cycle including auth middleware and error handling.
E2E Testing with Playwright
Playwright runs your tests in real browsers (Chromium, Firefox, WebKit) with a clean, async API. Test critical user journeys: sign up, log in, complete a purchase, submit a form. Use data-testid attributes instead of CSS selectors so tests don't break on styling changes. Run E2E tests in CI against a staging environment, not localhost.
Conclusion
A good test suite gives you confidence to refactor. Invest most of your testing effort at the unit and integration level where tests are fast and deterministic. Reserve E2E tests for the handful of flows that, if broken, would cause the most damage.