test_runner: merging coverage across test processes drops never-run blocks, so coverage gates pass
Version
v24.22.0, also v24.20.0 (the merge code is unchanged on main as of 2026-10-19)
Platform
Darwin 25.6.0 arm64 (the bug is in platform-independent JavaScript)
Subsystem
test_runner
What steps will reproduce the bug?
mod.mjs:
export const f = (a, b) => {
if (a) {
if (b) {
return "never";
}
return "a";
}
return "other";
};
a.test.mjs:
import assert from "node:assert";
import { test } from "node:test";
import { f } from "./mod.mjs";
test("a", () => {
assert.strictEqual(f(true, false), "a");
assert.strictEqual(f(false, false), "other");
});
b.test.mjs:
import assert from "node:assert";
import { test } from "node:test";
import { f } from "./mod.mjs";
test("b", () => {
assert.strictEqual(f(false, false), "other");
});
node --test --experimental-test-coverage --test-coverage-include=mod.mjs --test-coverage-branches=100 a.test.mjs
node --test --experimental-test-coverage --test-coverage-include=mod.mjs --test-coverage-branches=100 a.test.mjs b.test.mjs
How often does it reproduce? Is there a required condition?
Always. It needs two test processes (the default --test-isolation=process) where one runs a block but not a block nested in it, and the other runs neither.
What is the expected behavior? Why is that the expected behavior?
Both commands report the same coverage of mod.mjs, because b.test.mjs runs nothing that a.test.mjs does not: lines 4-5 uncovered, 80% branches, and the threshold fails with exit code 1. The first command does that, and so does the second with --test-isolation=none.
What do you see instead?
The second command reports 100% lines and branches and exits 0:
ℹ mod.mjs | 100.00 | 100.00 | 100.00 |
Adding a test file that covers less made a failing coverage gate pass.
Additional information
The raw V8 coverage of f (from NODE_V8_COVERAGE), per process:
a: [{17,120,2}, {38,100,1}, {51,80,0}, {100,119,1}]
b: [{17,120,1}, {38,100,0}]
merged by the test runner: [{17,120,3}, {38,100,1}, {100,119,1}]
V8 leaves out a block whose count equals its parent's, so b reports the whole if (a) block (38-100) as 0, and a reports the nested if (b) block (51-80) as 0. Neither process ran 51-80, but the merge drops it.
mergeCoverageRanges in lib/internal/test_runner/coverage.js merges ranges as sets, keeping a zero range only when the other process reports the same range or a zero range containing it. Both checks miss here, in either file order:
amerged first:b's 38-100 matches the old 38-100 exactly, and thebreakends the inner loop before the containment check reaches 51-80.bmerged first:a's 38-100 matches first and adds its count to the old 38-100 in place, so that range is no longer 0 whena's 51-80 is checked for containment.
The merge can drop zero ranges but never invents one, so the error always overstates coverage and thresholds pass. Line counts are also not summed unless ranges match exactly.
A merge that matches V8's own semantics sums, for each source position, the count of the innermost range that contains it in each process, as c8 does through mergeProcessCovs from @bcoe/v8-coverage.
We found this in Evolu, whose @evolu/sqlite-wasm package requires 100% line, branch, and function coverage from node:test across 26 test files. The gate passed while an out-of-memory branch in its database open ran in no test, which a single-file run showed. The same merge hid three more never-run blocks in Evolu's two coverage gates. It may belong on the coverage stabilization tracker, #63506.
I can reproduce this on v24.21.0, and in my setup the result is not just wrong but different from run to run with the same files, which makes a coverage threshold fail at random.
Environment
- node v24.21.0, Linux 6.12 x86_64 (Debian, in Docker), default
--test-isolation=process - also node v24.5.0, macOS (Darwin 25.6.0 arm64): same behaviour
Repro (6 files in one folder)
mod.mjs:
export function count(states) {
let delivered = 0;
for (const s of states) {
if (s === 'sent') {
continue;
}
if (s === 'delivered') {
delivered += 1;
}
}
return delivered;
}
export function other() {
return 1;
}
a.test.mjs:
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { count } from './mod.mjs';
test('a', () => {
assert.equal(count(['delivered', 'read']), 1);
assert.equal(count(['delivered']), 1);
});
b.test.mjs (loads the module, never calls count):
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { other } from './mod.mjs';
test('b', () => {
assert.equal(other(), 1);
});
c.test.mjs:
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { count } from './mod.mjs';
test('c', () => {
assert.equal(count(['read', 'delivered', 'delivered']), 2);
});
d.test.mjs:
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { count } from './mod.mjs';
test('d', () => {
assert.equal(count(['read']), 0);
});
e.test.mjs (calls count with an empty list, so the loop body never runs):
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { count } from './mod.mjs';
test('e', () => {
assert.equal(count([]), 0);
});
No test ever passes 'sent', so line 5 (continue;) is never executed.
for i in $(seq 1 30); do
node --test --test-concurrency=5 --experimental-test-coverage \
--test-reporter=lcov --test-reporter-destination=stdout \
a.test.mjs b.test.mjs c.test.mjs d.test.mjs e.test.mjs \
| grep -E '^(DA:5,|LH:)' | tr '\n' ' '; echo
done | sort | uniq -c
Expected: DA:5,0 and LH:14 in every run (that is what --test-isolation=none reports, 5 runs out of 5).
Actual, v24.21.0 on Linux, 30 runs:
26 DA:5,6 LH:16
4 DA:5,0 LH:14
On v24.5.0 on macOS, 30 runs: 27 times DA:5,6, 3 times DA:5,0.
So the never-run continue is reported as covered most of the time, with the count of the enclosing block, and as
uncovered some of the time. With a per-file threshold that was recorded from a lucky run, a later unrelated change
fails the gate on a file it does not touch.
Why it changes between runs, as far as I can read lib/internal/test_runner/coverage.js in v24.21.0:
getCoverageFromDirectoryreads the per-processcoverage-*.jsonfiles withopendirSync/readSyncand does not sort them, so the merge order follows the file system order of names that contain pids and timestamps;mergeCoverageRangesis not order-independent: it keeps old ranges with a count above zero, adds a new zero-count range only on an exact match, and between two nested zero-count ranges keeps the smaller one. Whether the zero-count block of thecontinuesurvives therefore depends on which process is merged first;mapRangeToLinesgives each line the count of the last range that fully covers it, so when that block is dropped, lines 5-6 inherit the loop body's count.
Adding a sixth file that does pass 'sent' (count(['sent', 'delivered'])) makes the result stable: 30 runs out of
30 report DA:5,1 LH:16 on v24.21.0, which matches the analysis above. A range-tree merge like the one in #66873 (the same approach as c8 and
@bcoe/v8-coverage) should also remove the run-to-run variation; it would be good if the PR's tests included a case
where the outcome depends on merge order, for example by merging the same set of process files in different orders.
I reproduced the original two-file example on Node v24.22.3, Linux x64. One additional diagnostic result: --test-concurrency=1 does not avoid the incorrect merged result.
| Configuration | Reported branch coverage | Exit status with 100% branch gate |
|---|---|---|
| a.test.mjs alone | 80% | 1 |
| a.test.mjs + b.test.mjs, default process isolation | 100% | 0 |
| Both files with --test-concurrency=1 | 100% | 0 |
| Both files with --test-isolation=none | 80% | 1 |
All runs used --experimental-test-coverage --test-coverage-include=mod.mjs --test-coverage-branches=100. The single-process result still reports lines 4-5 as uncovered.
This suggests that serializing test files is not a workaround for this reproducer: separate-process coverage still goes through the merge path. Including the serial-process configuration in a regression matrix could help distinguish merge correctness from test-execution concurrency. I have not tested the proposed fix in #66873.