Stop 12 · TestingTopics · 7/9

Test Coverage

Part of the Backend Developer Roadmap

In short

Coverage tells you which lines your tests actually ran. It's brilliant for spotting code nobody tests, and terrible as the only measure of whether your tests are any good.

A metric showing what percentage of your code is executed by your test suite — useful for finding untested areas, but a poor standalone measure of test quality.

The blob character paints a wall violet with a roller, leaving a few bare patches.

Think of it like this

Painting the wall

You roll paint over the wall and step back. Most of it is violet, but the corners by the switchboard are bare. Coverage shows you those bare patches. But paint over a crack still hides the crack. Running a line isn't the same as checking it works.

Watch it happen

Coverage of applyCoupon(), line by line

L1
0
L2
1
L3
2
L4
3
L5
4
L6
5
L7
6
L8
7

1/5applyCoupon has 8 lines. The test suite calls it with one valid coupon.

The big ideas

What you need to know

Four numbers

Statements, branches, functions and lines. Branch coverage, both sides of every if, usually tells you the most.

Use it to find gaps

Open the HTML report and look for red lines in important code, like payments and auth.

100% isn't the goal

A test can run a line without asserting anything. High coverage with weak assertions is false comfort.

Set a floor, not a target

A CI threshold like 80% stops coverage dropping, without pushing people to write pointless tests.

See it in code

Walk through the code

js
1// jest.config.js2export default {3  collectCoverage: true,4  collectCoverageFrom: ["src/**/*.ts", "!src/**/*.d.ts"],5  coverageReporters: ["text", "html"],6  coverageThreshold: {7    global: { branches: 75, lines: 80 },8    "./src/payments/": { branches: 95, lines: 95 },9  },10};

1/3Collect coverage from all source files, so untested files show up as 0% instead of vanishing.

Run npx jest, then open the HTML report in the coverage folder and click into a file to see uncovered lines in red.

Save yourself the pain

Common mistakes

Oops

Writing tests that call functions but assert nothing, just to raise coverage.

Do this instead

Coverage goes up and quality doesn't. Every test should check a result or a side effect.

Oops

Only looking at the overall percentage.

Do this instead

Look at which files and branches are uncovered. 90% overall can still mean the payment code is untested.

Your mission

Find your bare patches

~20 min

Quick check

A function's tests reach 100% line coverage. What does that guarantee?

Go deeper

  1. 1Run a coverage report on a project and identify the least-tested files.
  2. 2Add tests for one genuinely important untested code path, not just to raise the percentage.
  3. 3Learn why 100% coverage doesn't guarantee your tests actually catch bugs.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge