StoicBuilder

HandbookVIII the PalaestraCode coverage

VIIIthe Palaestra, Test it

Code coverage

1 min readUpdated 28 November

In plain words

Code coverage is the share of your lines and branches that ran at least once during the tests, which points at untested code but says nothing about whether the checks are any good.

Where you see it

Running npx vitest run --coverage prints a table with a row per file and columns for statements, branches, functions and lines. Harbor's invoice.ts shows 91% of lines but 58% of branches: the discount and credit paths rarely run.

Terminalterminal
$ npx vitest run --coverageFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #sprorate.ts | 100 | 100 | 100 | 100 |invoice.ts | 91.3 | 58.33 | 100 | 91.3 | 41-44,67refund.ts | 40 | 25 | 50 | 40 | 12-38
Uncovered Line #s points at code no test ever ran. Start with refund.ts: it handles money and is barely tested.

What goes wrong

  1. Chasing a number

    Ask the agent for 100% and it may write tests that call every function and check nothing. Coverage goes up and protection doesn't.

    test("covers refund", () => { refund(invoice); });
  2. Reading lines and ignoring branches

    An if with no else counts as fully covered lines even when the false case never runs. Branch coverage shows which side of each decision was never taken.

  3. Treating covered as correct

    A line can run in a test whose assertion is wrong or missing. Coverage finds untested code; only reading the tests tells you whether they're good.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Run the tests with coverage and list the files in lib/billing with the lowest branch coverage. For each uncovered branch that handles money or permissions, write a test with a specific expected value, and don't add tests that call code without checking a result.

Quick check

Sign in to earn +10 XP for it

Harbor's coverage report shows invoice.ts at 100% of lines. What can you conclude?

Got a prompt that touches code coverage?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.