Code coverage measures which parts of the code ran during tests: lines/statements, branches, functions. It's great at revealing what is not tested (a 0% file, an untaken else), but it doesn't prove tests are good: code can be executed without any meaningful assertion. Use coverage as a map for finding gaps and a guard against regressions on critical modules, not as a vanity number. Mutation testing is a better measure of whether tests actually catch bugs.
| Metric | Counts | Example gap it finds |
|---|---|---|
| Statement / line | Executed statements | Error-handling block never run |
| Branch | Each side of if, ?:, &&, ??, switch cases | discount ?? 0 never exercised with undefined |
| Function | Functions called at least once | Unused export, untested helper |
Branch coverage is the most informative; 100% line coverage can still miss half the branches.
function shippingCost(order) {
if (order.total > 100 || order.isMember) return 0;
return 7.99;
}
// One test with total=150 covers every line,
// but never checks isMember=true with a small total, or the paid case.
npx vitest run --coverage # V8 provider by default, or Istanbul
npx jest --coverage
npx playwright test # E2E coverage is possible via V8 coverage APIs
Reports show per-file percentages and highlight uncovered lines in HTML. Thresholds can fail CI:
// vitest.config.js
export default {
test: {
coverage: {
reporter: ['text', 'html', 'lcov'],
thresholds: { lines: 80, branches: 75 },
exclude: ['**/*.stories.*', '**/generated/**'],
},
},
};
V8 vs Istanbul: V8 uses the engine's built-in coverage (fast, no instrumentation); Istanbul instruments code (historically more precise for branches). Modern Vitest's V8 provider remaps results to be very close to Istanbul's accuracy.
test('calculates tax', () => {
calculateTax(100, 'DE'); // 100% coverage, zero assertions
});
Goodhart's law applies: when coverage becomes a target, people write assertion-free tests and test trivial code to hit the number.
> to >=, remove a call) and checks whether a test fails. Surviving mutants show weak tests. Run it on critical modules; it's slow on large codebases.Spotted a mistake or a rights concern? Report an issue