Skip to content

test_runner: merging coverage across test processes drops never-run blocks, so coverage gates pass #66860

Open

Description

@steida

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:

  • a merged first: b's 38-100 matches the old 38-100 exactly, and the break ends the inner loop before the containment check reaches 51-80.
  • b merged 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 when a'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.

Activity

  1. adoslabsproject-gif commented on Nov 9, 2026

    @adoslabsproject-gif

    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:

    • getCoverageFromDirectory reads the per-process coverage-*.json files with opendirSync/readSync and does not
      sort them, so the merge order follows the file system order of names that contain pids and timestamps;
    • mergeCoverageRanges is 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 the continue survives therefore depends on which process is merged first;
    • mapRangeToLines gives 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions