You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Automated testing is the most critical quality gate in a CI/CD pipeline. Without reliable tests, you cannot confidently ship changes. This lesson covers testing levels, strategies, and how to integrate tests into your pipeline.
Manual testing is:
Automated testing is:
The testing pyramid is a widely accepted model for balancing different types of tests:
╱╲
╱ E2E ╲ Few, slow, expensive
╱────────╲
╱Integration╲ Some, moderate speed
╱──────────────╲
╱ Unit Tests ╲ Many, fast, cheap
╱──────────────────╲
| Level | Quantity | Speed | Cost | Scope |
|---|---|---|---|---|
| Unit | Many (hundreds+) | Very fast (ms) | Low | Single function / class |
| Integration | Some (tens) | Moderate (seconds) | Medium | Multiple components |
| End-to-End (E2E) | Few (< 20) | Slow (minutes) | High | Full user workflow |
Test a single function, method, or component in isolation. Dependencies are mocked.
// sum.ts
export function sum(a: number, b: number): number {
return a + b;
}
// sum.test.ts
import { sum } from './sum';
describe('sum', () => {
it('adds two positive numbers', () => {
expect(sum(2, 3)).toBe(5);
});
it('handles negative numbers', () => {
expect(sum(-1, 1)).toBe(0);
});
it('handles zero', () => {
expect(sum(0, 0)).toBe(0);
});
});
Test how multiple components work together — e.g., API routes with a database.
// api.test.ts
import { createServer } from '../server';
import { prisma } from '../db';
describe('POST /api/users', () => {
afterAll(() => prisma.$disconnect());
it('creates a user and returns 201', async () => {
const app = createServer();
const res = await app.inject({
method: 'POST',
url: '/api/users',
payload: { name: 'Alice', email: 'alice@example.com' },
});
expect(res.statusCode).toBe(201);
expect(res.json()).toMatchObject({ name: 'Alice' });
});
});
Test the full application from the user's perspective, typically using a browser.
// login.e2e.ts (Playwright)
import { test, expect } from '@playwright/test';
test('user can log in', async ({ page }) => {
await page.goto('http://localhost:3000/login');
await page.fill('[name="email"]', 'alice@example.com');
await page.fill('[name="password"]', 'secret123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('h1')).toContainText('Welcome, Alice');
});
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.