Today's Objective
By the end of this lesson you will understand tdd and coverage deeply enough to apply it immediately in real projects.
// TDD: Write the test FIRST — it will fail (Red)
// src/cart.test.js
import { Cart } from './cart';
describe('Cart', () => {
let cart;
beforeEach(() => { cart = new Cart(); });
test('starts empty', () => {
expect(cart.items).toHaveLength(0);
expect(cart.total).toBe(0);
});
test('adds items', () => {
cart.add({ id: 1, name: 'Widget', price: 9.99 });
expect(cart.items).toHaveLength(1);
expect(cart.total).toBeCloseTo(9.99);
});
test('removes items', () => {
cart.add({ id: 1, name: 'Widget', price: 9.99 });
cart.remove(1);
expect(cart.items).toHaveLength(0);
});
test('applies discount', () => {
cart.add({ id: 1, name: 'Widget', price: 100 });
cart.applyDiscount(10); // 10%
expect(cart.total).toBeCloseTo(90);
});
});
// Now write the minimum code to make tests pass (Green)
// src/cart.js
export class Cart {
constructor() { this._items = []; this._discount = 0; }
get items() { return [...this._items]; }
get total() {
const subtotal = this._items.reduce((s, i) => s + i.price, 0);
return subtotal * (1 - this._discount / 100);
}
add(item) { this._items.push(item); }
remove(id) { this._items = this._items.filter(i => i.id !== id); }
applyDiscount(pct) { this._discount = pct; }
}
Tip: Coverage shows which lines ran, not whether behaviour is correct. Aim for high coverage on business logic; don't obsess over 100%.
Exercise: TDD a Feature
- Write failing tests for a Checkout class (subtotal/tax/total)
- Run tests and confirm they are red
- Write the minimum code to turn them green
- Refactor without breaking tests
- Generate a coverage report and identify uncovered branches
Day 5 Summary
- Red-Green-Refactor is the TDD cycle
- Write the simplest code that passes — no more
- Refactor only when tests are green
- Branch coverage checks every if/else path
- TDD produces smaller, more testable functions by design
← Previous
End-to-End Testing with Playwright
Course Complete 🎉
Back to Software Testing in 5 Days
Course Complete
Completing all five days means having a solid working knowledge of Testing. The skills here translate directly to real projects. The next step is practice — pick a project and build something with what was learned.
Supporting Videos & Reading
Go deeper with these external references.
Day 5 Checkpoint
Before moving on, verify you can answer these without looking:
- What is the core concept introduced in this lesson, and why does it matter?
- What are the two or three most common mistakes practitioners make with this topic?
- Can you explain the key code pattern from this lesson to a colleague in plain language?
- What would break first if you skipped the safeguards or best practices described here?
- How does today's topic connect to what comes in Day the final lesson?
Live Bootcamp
Learn this in person — 2 days, 5 cities
Thu–Fri sessions in Denver, Los Angeles, New York, Chicago, and Dallas. $1,490 per seat. June–October 2026.
Reserve Your Seat →
Back to Course
Testing — Full Course Overview
→