Skip to content
jshacks.dev
Testing

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.

1 min read
Summarize in AI

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.

math.mjsJavaScript
export function sign(n) {
  if (n > 0) return 'positive';
  if (n < 0) return 'negative';
  return 'zero';
}
math.test.mjsJavaScript
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');
});
Shell
node --test --experimental-test-coverage
Text
✔ 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 report

The 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.

Also inNode.js
Taggednode 24
Share

More snippets

Testing

Group tests with describe in node test

Group related tests with describe and it from node:test, nest suites, and run setup with beforeEach, all without installing a framework.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.