Coverage and test quality
Prerequisite: node-fixtures-and-mocking. Objective: Identify untested boundary. Local baseline: Node.js 22.16.0 on Linux, ESM. Each example is independent: save as main.mjs in a disposable directory and run node main.mjs. The author harness uses a five-second timeout. Static snippets are not ByteMind interactive execution.
How it works
Line, function and branch coverage describe different execution units. A line containing a conditional expression can execute without both paths being exercised. Node 22.16.0 offers experimental test coverage through --experimental-test-coverage; inspect actual runner flags before using thresholds. A boundary table and a deliberately incorrect implementation can reveal missing assertions. Mutation testing changes code to see whether tests detect it; a single manual mutant is only a small example of that method.
Show a boundary test killing an off-by-one mutant
const good=n=>n>=10;const wrong=n=>n>10;const cases=[[9,false],[10,true],[11,true]];for(const [name,fn] of [['correct',good],['mutant',wrong]]) console.log(name,cases.every(([n,want])=>fn(n)===want));Expected output:
correct truemutant falseThe exact threshold distinguishes two plausible implementations.
Observe the built-in coverage report
import {spawnSync} from 'node:child_process';import {mkdtempSync,writeFileSync,rmSync} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';const dir=mkdtempSync(join(tmpdir(),'bm-cov-'));try{const f=join(dir,'coverage.test.mjs');writeFileSync(f,"import test from 'node:test';import assert from 'node:assert/strict';function fee(n){if(n>=10)return 0;return 5;}test('large',()=>assert.equal(fee(12),0));");const r=spawnSync(process.execPath,['--test','--experimental-test-coverage',f],{encoding:'utf8',timeout:2000});console.log('exit',r.status);console.log('coverage report',r.stdout.includes('coverage'));}finally{rmSync(dir,{recursive:true,force:true});}Expected output:
exit 0coverage report trueThe runner emits coverage information despite the missing small-order assertion. No percentage is treated as a quality guarantee.
Common mistakes
Do not confuse a high coverage percentage with correctness. Test exactly at boundaries and include errors and empty inputs, even when every line already ran.
Practice
Add quantity 9, 10 and 11 to a discount test and explain which off-by-one implementation they reject.
Reference candidate: node-coverage-and-test-quality-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/test.html. Selected versioned documentation opened.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Coverage and test quality — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What does line coverage establish?
Which case distinguishes >=10 from >10?
What does a surviving deliberate mutant indicate?