Skip to content

Introduction to Testing

Testing your Next.js application involves multiple levels, each with different tradeoffs between speed and confidence.

flowchart TB
subgraph Fast
UT[Unit Tests] -->|Test individual functions| HighSpeed[Milliseconds]
end
subgraph Medium
IT[Integration Tests] -->|Test features together| MedSpeed[Seconds]
end
subgraph Slow
E2E[End-to-End Tests] -->|Test complete user flows| LowSpeed[Minutes]
end
UT --> IT --> E2E
Test TypeWhat It TestsSpeedFrequency
UnitFunctions, utilities, small componentsFast (ms)Every commit
IntegrationPages, API routes, featuresMedium (s)Every commit
E2EComplete user flows (login, checkout)Slow (min)Before deploy
Terminal window
# Unit & Integration Tests
npm install vitest @testing-library/react @testing-library/jest-dom
# E2E Tests
npm install -D @playwright/test
npx playwright install
vitest.config.ts
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import path from 'path'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
setupFiles: ['./tests/setup.ts'],
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
})

Use a mix of test types: fast unit tests for individual functions, integration tests for features, and a few critical E2E tests for complete user flows. Run fast tests on every commit, E2E tests before deployment.