Run tests with coverage using node --experimental-test-coverage
Print a coverage table for node:test with the experimental test coverage flag, then enforce a minimum with the coverage threshold flags.
Node.js 24 can report which lines your tests ran. Add one flag to node --test and the coverage table appears after the test results.
export function sign(n) {
if (n > 0) return 'positive';
if (n < 0) return 'negative';
return 'zero';
}import test from 'node:test';
import assert from 'node:assert/strict';
import { sign } from './math.mjs';
test('sign of a positive number', () => {
assert.equal(sign(5), 'positive');
});node --test --experimental-test-coverage✔ sign of a positive number (0.6ms)
ℹ start of coverage report
ℹ file | line % | branch % | funcs % | uncovered lines
ℹ math.mjs | 57.14 | 33.33 | 100.00 | 3-4
ℹ all files | 57.14 | 33.33 | 100.00 |
ℹ end of coverage reportThe table layout and the timings are approximate and vary by Node.js version, which draws ruled lines around the columns. The numbers follow from the code: only the first return runs, so lines 3 and 4 are uncovered, and one of the three branches was taken. The flag is described in the test runner docs.
Add --test-coverage-lines=90 to fail the run below 90 percent, and --test-coverage-exclude to skip files. The feature still carries the experimental name, and it counts your test files too unless you exclude them. Coverage shows that code ran, not that anything was asserted about it.