Coverage
Coverage measures which lines, branches, and functions ran during tests. It is a map of execution, not a map of correctness.
Detailed Explanation
A coverage tool instruments your source (Istanbul) or uses V8's built-in counters (v8 provider in Node, Vitest, c8). After the suite, it writes an HTML report and a percentage: statements, branches, functions, lines. Branches are the important number: an if you never took false is a gap.
Jest: jest --coverage. Vitest: vitest --coverage with @vitest/coverage-v8 or istanbul. Node: npx c8 node --test. CI often fails under a threshold (coverageThreshold in Jest). Thresholds should be local to changed packages, or they become a reason to write expect(true) tests.
Coverage does not mean the assertion was meaningful. A line that ran can still be wrong. 100% coverage on a file that never tests the error path is a false comfort if branches are ignored. Conversely, 70% with the payment and auth paths tested can be healthier than 95% of getters.
Exclude generated files, *.d.ts, and barrels that only re-export. Include the source that can break users. Do not chase coverage on trivial wrappers.
What it costs you: time (instrumentation), noise (reports in PRs), and perverse incentives (tests written for the meter). Treat uncovered branches in critical modules as a todo, not a vanity percentage.
Reach for coverage when you want to see what a suite never touches. Reach for mutation testing (Stryker) when you want to know if assertions actually catch bugs. Reach for a checklist of user flows when the risk is integration, not a missed line.
Diff coverage (only lines you touched) is a kinder gate than a global 90%. A legacy module at 40% should not block a one-line bugfix in a well-tested file. Many CI systems compute that from lcov plus the PR diff.
Ignore comments are a smell when they sit on branches you could hit with one extra test. They are reasonable on generated exhaustiveness never checks and on browser-only stubs in a Node test run.
Code Examples
export function fee(cents, vip) {
if (vip) return 0;
return Math.ceil(cents * 0.03);
}
// A test that only calls fee(100, true) covers the early return.
// Branch coverage still flags the non-vip path until you call fee(100, false).npx jest --coverage
npx vitest run --coverage
npx c8 --reporter=text npm testjest.config.js excerpt
// jest.config.js excerpt
export default {
coverageThreshold: {
global: { branches: 70, lines: 80 },
},
};Common Gotchas
Line coverage can be high while a branch is untested
read the HTML highlight, not only the badge.
Thresholds on a monorepo global average hide a 20% package
set per-project gates.