Testing: What to Test and How
Not all tests are equal. A unit test that verifies a pure function is fast and stable. An E2E test that clicks through a browser is slow and flaky. The trick is getting the right balance.
The Test Pyramid
/ E2E \ Few, slow, expensive
/ Integration \ Medium amount
/ Unit Tests \ Many, fast, cheap| Level | What It Tests | Speed | Confidence | Maintenance |
|---|---|---|---|---|
| Unit | Individual functions/classes | Milliseconds | Low (mocked deps) | Low |
| Integration | Modules working together | Seconds | Medium | Medium |
| E2E | Full user flows | Seconds-Minutes | High | High |
Unit Tests (Vitest/Jest)
// Pure function — perfect for unit testing
function calculateTax(income: number, rate: number): number {
if (income <= 0) return 0;
return income * rate;
}
// Test
describe('calculateTax', () => {
it('calculates tax correctly', () => {
expect(calculateTax(1000, 0.15)).toBe(150);
});
it('returns 0 for zero income', () => {
expect(calculateTax(0, 0.15)).toBe(0);
});
it('returns 0 for negative income', () => {
expect(calculateTax(-100, 0.15)).toBe(0);
});
});Integration Tests
// Testing API route with real database
import { createServer } from '../server';
import { db } from '../database';
describe('POST /api/users', () => {
beforeEach(async () => {
await db.users.deleteAll(); // clean slate
});
it('creates a user and returns 201', async () => {
const res = await request(app)
.post('/api/users')
.send({ name: 'Alice', email: 'alice@example.com' })
.expect(201);
expect(res.body.name).toBe('Alice');
// Verify it's actually in the database
const user = await db.users.findByEmail('alice@example.com');
expect(user).toBeDefined();
});
it('returns 422 for invalid email', async () => {
await request(app)
.post('/api/users')
.send({ name: 'Alice', email: 'not-an-email' })
.expect(422);
});
});E2E Tests (Playwright)
import { test, expect } from '@playwright/test';
test('user can sign up and see dashboard', async ({ page }) => {
await page.goto('/signup');
await page.fill('[name=email]', 'test@example.com');
await page.fill('[name=password]', 'SecureP@ss123');
await page.click('button[type=submit]');
// Should redirect to dashboard
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('h1')).toContainText('Welcome');
});What to Test at Each Level
| Unit | Integration | E2E |
|---|---|---|
| Pure functions | API endpoints | Critical user flows |
| Data transformations | Database queries | Sign up / Login |
| Validation logic | Auth middleware | Checkout / Payment |
| State reducers | Service interactions | Form submissions |
| Utility helpers | WebSocket handlers | Navigation flows |
Testing Anti-Patterns
- Testing implementation, not behavior. Don't test that a function calls another function. Test the output.
- 100% coverage as a goal. Coverage measures lines executed, not correctness. 80% with good tests beats 100% with trivial ones.
- Mocking everything. If your unit test mocks 5 dependencies, it's testing the mocks, not the code.
- Slow tests you don't run. Tests only help if you run them. Keep the suite fast.
Format your test data: JSON Formatter — format test fixtures and mock data.