Skip to content

Coverage-Driven Testing

Medium ~40 min

What you'll build

A test suite for a discount calculator module, driven by coverage analysis — starting with a naive test, then adding tests to cover all branches.

Goals

  • Generate and read coverage reports
  • Identify uncovered branches and lines
  • Write targeted tests for coverage gaps
  • Understand the difference between line and branch coverage

Prerequisites

  • Basic testing
  • Conditional logic in functions
  • Understanding of coverage concepts

Steps

  1. 1

    Create the module to test

    Create a discount calculator with multiple branches: different customer tiers, seasonal sales, and minimum purchase thresholds.

    Code
    // src/utils/discount.ts
    export function calculateDiscount(
      price: number,
      tier: 'regular' | 'silver' | 'gold',
      hasCoupon: boolean,
      isHolidaySeason: boolean
    ): number {
      if (price < 0) throw new Error('Price must be non-negative');
    
      let discount = 0;
    
      if (tier === 'gold') discount = 0.20;
      else if (tier === 'silver') discount = 0.10;
    
      if (hasCoupon && price > 100) discount += 0.05;
    
      if (isHolidaySeason) discount += 0.10;
    
      if (discount > 0.40) discount = 0.40; // cap at 40%
    
      return Math.round(price * discount * 100) / 100;
    }

    Expected result

    A discount function with multiple branches and a cap.

  2. 2

    Write a naive test and check coverage

    Write one basic test, then run with --coverage to see the coverage report. Identify uncovered branches.

    Code
    import { calculateDiscount } from '../utils/discount';
    
    describe('calculateDiscount', () => {
      it('gives gold tier 20% off', () => {
        expect(calculateDiscount(100, 'gold', false, false)).toBe(20);
      });
    });
    
    // Run: npx jest --coverage
    // You'll see many uncovered branches: silver, coupon, holiday, cap, error.

    Expected result

    Coverage report shows low branch coverage — many paths are untested.

  3. 3

    Add tests for each tier

    Add tests for the 'regular' and 'silver' tiers to cover those branches. Check coverage again.

  4. 4

    Cover the coupon and holiday branches

    Add tests for the coupon branch (price > 100 with coupon) and the holiday season branch. These add 5% and 10% respectively.

  5. 5

    Cover the cap and error branches

    Add tests for the 40% cap (gold + coupon + holiday > 40%) and the negative price error. These are the last uncovered branches.

    Code
    it('caps discount at 40%', () => {
      // gold (20%) + coupon (5%) + holiday (10%) = 35%, under cap
      expect(calculateDiscount(200, 'gold', true, true)).toBe(70);
      // gold (20%) + holiday (10%) = 30%, but with a high price
      // the cap triggers when discount > 0.40
    });
    
    it('throws on negative price', () => {
      expect(() => calculateDiscount(-1, 'regular', false, false)).toThrow('Price must be non-negative');
    });

    Expected result

    Coverage reaches 100% line and branch — all paths are tested.

Try it yourself

What you learned

You used coverage as a guide to find untested code paths. Coverage-driven testing ensures every branch is exercised — but remember, high coverage doesn't guarantee good tests; the assertions must be meaningful.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic