Complete guide to testing your applications.
Test individual functions and components in isolation.
// Example: sum.test.ts
import { sum } from './sum';
describe('sum function', () => {
it('should add two numbers correctly', () => {
expect(sum(2, 3)).toBe(5);
});
it('should handle negative numbers', () => {
expect(sum(-2, 3)).toBe(1);
});
});
Test how different parts work together.
// Example: api.test.ts
import request from 'supertest';
import app from './app';
describe('User API', () => {
it('should create a new user', async () => {
const response = await request(app)
.post('/api/users')
.send({ name: 'John', email: 'john@example.com' });
expect(response.status).toBe(201);
expect(response.body).toHaveProperty('id');
});
});
Test complete user workflows.
// Example: user-flow.spec.ts
import { test, expect } from '@playwright/test';
test('user can sign up and log in', async ({ page }) => {
await page.goto('http://localhost:3000');
await page.click('text=Sign Up');
await page.fill('input[name="email"]', 'test@example.com');
await page.fill('input[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('/dashboard');
});
npm install -D jest ts-jest @types/jest
npx ts-jest config:init
npm install -D @testing-library/react @testing-library/jest-dom
npm init playwright@latest
# Run all tests
npm test
# Watch mode
npm test -- --watch
# Coverage report
npm test -- --coverage
# Specific file
npm test -- user.test.ts
Tests run automatically on: