Skip to content

Commit 20a93ab

Browse files
MoLownodejs-github-bot
authored andcommitted
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: #67512 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 19cb394 commit 20a93ab

9 files changed

Lines changed: 72 additions & 4 deletions

File tree

‎doc/api/test.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ create it.
135135
the state file is a JSON file that contains an array of run attempts.
136136
Each run attempt is an object mapping successful tests to the attempt they have passed in.
137137
The key identifying a test in this map is the test file path, with the line and column where the test is defined.
138+
When tests run with process isolation and a test is defined outside of the test file being run,
139+
for example in a helper shared by several test files,
140+
the key is prefixed with the path of that test file.
138141
in a case where a test defined in a specific location is run multiple times,
139142
for example within a function or a loop,
140143
a counter will be appended to the key, to disambiguate the test runs.

‎lib/internal/test_runner/harness.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function createTestTree(rootTestOptions, globalOptions) {
6868
teardown: null,
6969
snapshotManager: null,
7070
previousRuns: null,
71+
rerunEntryFile: null,
7172
isFilteringByName,
7273
isFilteringByOnly,
7374
isFilteringByTags,
@@ -219,6 +220,10 @@ function setupFailureStateFile(rootTest, globalOptions) {
219220
if (!process.env.NODE_TEST_CONTEXT) {
220221
const reporter = reportReruns(rootTest.harness.previousRuns, globalOptions);
221222
compose(rootTest.reporter, reporter).pipe(new PassThrough());
223+
} else {
224+
// The state is written by the parent process, which scopes the tests of a
225+
// child process running a single file to that file.
226+
rootTest.harness.rerunEntryFile = rootTest.entryFile;
222227
}
223228
}
224229

‎lib/internal/test_runner/reporter/rerun.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const {
55
ArrayPrototypePush,
66
JSONStringify,
77
} = primordials;
8-
const { relative } = require('path');
98
const { writeFileSync } = require('fs');
9+
const { getRerunTestId } = require('internal/test_runner/utils');
1010

1111
function reportReruns(previousRuns, globalOptions) {
1212
return async function reporter(source) {
@@ -16,7 +16,7 @@ function reportReruns(previousRuns, globalOptions) {
1616
const roots = [];
1717

1818
function getTestId(data) {
19-
return `${relative(globalOptions.cwd, data.file)}:${data.line}:${data.column}`;
19+
return getRerunTestId(globalOptions.cwd, data, data.entryFile);
2020
}
2121

2222
function startTest(data) {

‎lib/internal/test_runner/test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const {
7070
createDeferredCallback,
7171
createSeededGenerator,
7272
countCompletedTest,
73+
getRerunTestId,
7374
isTestFailureError,
7475
reporterScope,
7576
testChannel,
@@ -97,7 +98,6 @@ const {
9798
} = require('timers');
9899
const { TIMEOUT_MAX } = require('internal/timers');
99100
const { fileURLToPath } = require('internal/url');
100-
const { relative } = require('path');
101101
const { availableParallelism } = require('os');
102102
const { innerOk } = require('internal/assert/utils');
103103
const { bigint: hrtime } = process.hrtime;
@@ -837,7 +837,7 @@ class Test extends AsyncResource {
837837
}
838838

839839
if (this.loc != null && this.root.harness.previousRuns != null) {
840-
const baseIdentifier = `${relative(this.config.cwd, this.loc.file)}:${this.loc.line}:${this.loc.column}`;
840+
const baseIdentifier = getRerunTestId(this.config.cwd, this.loc, this.root.harness.rerunEntryFile);
841841
let testIdentifier = baseIdentifier;
842842
const disambiguator = this.root.testDisambiguator.get(baseIdentifier);
843843
if (disambiguator !== undefined) {

‎lib/internal/test_runner/utils.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ function parsePreviousRuns(rerunFailuresFilePath) {
197197
return JSONParse(data);
198198
}
199199

200+
// A location outside of the entry file is shared by every test file that
201+
// reaches it, so it is scoped to the entry file when there is one.
202+
function getRerunTestId(cwd, { file, line, column }, entryFile) {
203+
const id = `${relative(cwd, file)}:${line}:${column}`;
204+
if (entryFile == null || entryFile === file) {
205+
return id;
206+
}
207+
return `${relative(cwd, entryFile)}::${id}`;
208+
}
209+
200210
async function getReportersMap(reporters, destinations) {
201211
return SafePromiseAllReturnArrayLike(reporters, async (name, i) => {
202212
const destination = kBuiltinDestinations.get(destinations[i]) ??
@@ -741,6 +751,7 @@ module.exports = {
741751
testStorage,
742752
shouldColorizeTestFiles,
743753
getCoverageReport,
754+
getRerunTestId,
744755
setupGlobalSetupTeardownFunctions,
745756
parsePreviousRuns,
746757
testChannel,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { describeEngine } from './helper.mjs';
2+
3+
describeEngine('a-1');
4+
describeEngine('a-2');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { describeEngine } from './helper.mjs';
2+
3+
describeEngine('b-1');
4+
describeEngine('b-2');
5+
describeEngine('b-3');
6+
describeEngine('b-4', { fails: true });
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
3+
4+
// Every test file calling this registers its suites and tests at the same
5+
// source location, which is outside of the test file itself.
6+
export function describeEngine(name, { fails = false } = {}) {
7+
describe(name, () => {
8+
it('works', ({ attempt }) => {
9+
assert.ok(!fails, `${name} always fails`);
10+
assert.strictEqual(attempt, 0, `${name} already passed and must not run again`);
11+
});
12+
});
13+
}

‎test/parallel/test-runner-test-rerun-failures.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,32 @@ test('failing test is not swallowed when siblings share its source location', as
232232
assertAttempt(state, 1);
233233
});
234234

235+
{
236+
// Both fixtures register all of their suites and tests from the same
237+
// location in a shared helper. In each mode, the suite that fails must keep
238+
// failing on the next attempt, and the ones that passed must be replayed.
239+
const a = fixtures.path('test-runner', 'rerun-shared-helper-across-files', 'a.mjs');
240+
const b = fixtures.path('test-runner', 'rerun-shared-helper-across-files', 'b.mjs');
241+
const modes = [
242+
{ name: 'process isolation', args: ['--test', a, b], pass: 5 },
243+
{ name: 'no isolation', args: ['--test', '--test-isolation=none', a, b], pass: 5 },
244+
{ name: 'a file run without --test', args: [b], pass: 3 },
245+
];
246+
247+
for (const { name, args, pass } of modes) {
248+
test(`failing test in a helper shared by test files is not swallowed with ${name}`, async () => {
249+
for (let attempt = 0; attempt < 2; attempt++) {
250+
const { code, signal, stdout } = await common.spawnPromisified(
251+
process.execPath, ['--test-rerun-failures', stateFile, ...args]);
252+
assert.strictEqual(signal, null);
253+
assert.match(stdout, new RegExp(`^ℹ pass ${pass}$`, 'm'), `attempt ${attempt}`);
254+
assert.match(stdout, /^ℹ fail 1$/m, `attempt ${attempt}`);
255+
assert.strictEqual(code, 1, `attempt ${attempt}`);
256+
}
257+
});
258+
}
259+
}
260+
235261
test('rerun preserves the original duration on the replayed pass', async () => {
236262
const durationFixture = fixtures.path('test-runner', 'rerun-duration.js');
237263
const args = ['--test-rerun-failures', stateFile, durationFixture];

0 commit comments

Comments
 (0)