test_runner: scope rerun ids to the entry file

With process isolation, the `--test-rerun-failures` state file is
written by the parent process, which numbered tests sharing a location
across all test files, while each child process numbers them within
its own file. A test defined in a helper used by several test files
was therefore looked up in a slot belonging to another file, and a
failing test could be replayed as passed without running.

Prefix the identifier of a test defined outside of its entry file with
the entry file when tests run in per-file child processes, and build
the identifier with the same helper on both sides.

Signed-off-by: Moshe Atlow <moshe@atlow.co.il>
PR-URL: https://github.com/nodejs/node/pull/67512
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Moshe Atlow authored and Antoine du Hamel committed 2026-11-19 15:44:16 +01:00
1 parent 4394bf9cfb
commit c871b8bdad
9 files changed
+72 -4

No files matched your search

+3
View File
@@ -135,6 +135,9 @@ create it.
the state file is a JSON file that contains an array of run attempts.
Each run attempt is an object mapping successful tests to the attempt they have passed in.
The key identifying a test in this map is the test file path, with the line and column where the test is defined.
When tests run with process isolation and a test is defined outside of the test file being run,
for example in a helper shared by several test files,
the key is prefixed with the path of that test file.
in a case where a test defined in a specific location is run multiple times,
for example within a function or a loop,
a counter will be appended to the key, to disambiguate the test runs.
+5
View File
@@ -68,6 +68,7 @@ function createTestTree(rootTestOptions, globalOptions) {
teardown: null,
snapshotManager: null,
previousRuns: null,
rerunEntryFile: null,
isFilteringByName,
isFilteringByOnly,
isFilteringByTags,
@@ -219,6 +220,10 @@ function setupFailureStateFile(rootTest, globalOptions) {
if (!process.env.NODE_TEST_CONTEXT) {
const reporter = reportReruns(rootTest.harness.previousRuns, globalOptions);
compose(rootTest.reporter, reporter).pipe(new PassThrough());
} else {
// The state is written by the parent process, which scopes the tests of a
// child process running a single file to that file.
rootTest.harness.rerunEntryFile = rootTest.entryFile;
}
}
+2 -2
View File
@@ -5,8 +5,8 @@ const {
ArrayPrototypePush,
JSONStringify,
} = primordials;
const { relative } = require('path');
const { writeFileSync } = require('fs');
const { getRerunTestId } = require('internal/test_runner/utils');
function reportReruns(previousRuns, globalOptions) {
return async function reporter(source) {
@@ -16,7 +16,7 @@ function reportReruns(previousRuns, globalOptions) {
const roots = [];
function getTestId(data) {
return `${relative(globalOptions.cwd, data.file)}:${data.line}:${data.column}`;
return getRerunTestId(globalOptions.cwd, data, data.entryFile);
}
function startTest(data) {
+2 -2
View File
@@ -70,6 +70,7 @@ const {
createDeferredCallback,
createSeededGenerator,
countCompletedTest,
getRerunTestId,
isTestFailureError,
reporterScope,
testChannel,
@@ -97,7 +98,6 @@ const {
} = require('timers');
const { TIMEOUT_MAX } = require('internal/timers');
const { fileURLToPath } = require('internal/url');
const { relative } = require('path');
const { availableParallelism } = require('os');
const { innerOk } = require('internal/assert/utils');
const { bigint: hrtime } = process.hrtime;
@@ -837,7 +837,7 @@ class Test extends AsyncResource {
}
if (this.loc != null && this.root.harness.previousRuns != null) {
const baseIdentifier = `${relative(this.config.cwd, this.loc.file)}:${this.loc.line}:${this.loc.column}`;
const baseIdentifier = getRerunTestId(this.config.cwd, this.loc, this.root.harness.rerunEntryFile);
let testIdentifier = baseIdentifier;
const disambiguator = this.root.testDisambiguator.get(baseIdentifier);
if (disambiguator !== undefined) {
+11
View File
@@ -197,6 +197,16 @@ function parsePreviousRuns(rerunFailuresFilePath) {
return JSONParse(data);
}
// A location outside of the entry file is shared by every test file that
// reaches it, so it is scoped to the entry file when there is one.
function getRerunTestId(cwd, { file, line, column }, entryFile) {
const id = `${relative(cwd, file)}:${line}:${column}`;
if (entryFile == null || entryFile === file) {
return id;
}
return `${relative(cwd, entryFile)}::${id}`;
}
async function getReportersMap(reporters, destinations) {
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
const destination = kBuiltinDestinations.get(destinations[i]) ??
@@ -741,6 +751,7 @@ module.exports = {
testStorage,
shouldColorizeTestFiles,
getCoverageReport,
getRerunTestId,
setupGlobalSetupTeardownFunctions,
parsePreviousRuns,
testChannel,
@@ -0,0 +1,4 @@
import { describeEngine } from './helper.mjs';
describeEngine('a-1');
describeEngine('a-2');
@@ -0,0 +1,6 @@
import { describeEngine } from './helper.mjs';
describeEngine('b-1');
describeEngine('b-2');
describeEngine('b-3');
describeEngine('b-4', { fails: true });
@@ -0,0 +1,13 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
// Every test file calling this registers its suites and tests at the same
// source location, which is outside of the test file itself.
export function describeEngine(name, { fails = false } = {}) {
describe(name, () => {
it('works', ({ attempt }) => {
assert.ok(!fails, `${name} always fails`);
assert.strictEqual(attempt, 0, `${name} already passed and must not run again`);
});
});
}
@@ -232,6 +232,32 @@ test('failing test is not swallowed when siblings share its source location', as
assertAttempt(state, 1);
});
{
// Both fixtures register all of their suites and tests from the same
// location in a shared helper. In each mode, the suite that fails must keep
// failing on the next attempt, and the ones that passed must be replayed.
const a = fixtures.path('test-runner', 'rerun-shared-helper-across-files', 'a.mjs');
const b = fixtures.path('test-runner', 'rerun-shared-helper-across-files', 'b.mjs');
const modes = [
{ name: 'process isolation', args: ['--test', a, b], pass: 5 },
{ name: 'no isolation', args: ['--test', '--test-isolation=none', a, b], pass: 5 },
{ name: 'a file run without --test', args: [b], pass: 3 },
];
for (const { name, args, pass } of modes) {
test(`failing test in a helper shared by test files is not swallowed with ${name}`, async () => {
for (let attempt = 0; attempt < 2; attempt++) {
const { code, signal, stdout } = await common.spawnPromisified(
process.execPath, ['--test-rerun-failures', stateFile, ...args]);
assert.strictEqual(signal, null);
assert.match(stdout, new RegExp(`^ℹ pass ${pass}$`, 'm'), `attempt ${attempt}`);
assert.match(stdout, /^ℹ fail 1$/m, `attempt ${attempt}`);
assert.strictEqual(code, 1, `attempt ${attempt}`);
}
});
}
}
test('rerun preserves the original duration on the replayed pass', async () => {
const durationFixture = fixtures.path('test-runner', 'rerun-duration.js');
const args = ['--test-rerun-failures', stateFile, durationFixture];