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?
Back to Course
Testing — Full Course Overview
→
“Whatever you do, work at it with all your heart.” — Colossians 3:23
“For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life.” — John 3:16