Testing

Coverage and test quality

Code coverage measures which instrumented parts of a program ran during tests. It cannot determine whether assertions were useful or every important input was tested. 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. 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. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/test.html.

Formal Syntax & Structure

nodejs syntaxnodejs
node --test --experimental-test-coverage file.test.mjs

Working Example

Example Snippetnodejs
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));
Node.js Reference