diff --git a/lib/internal/test_runner/reporter/rerun.js b/lib/internal/test_runner/reporter/rerun.js index 9658a7fb70f..ecdd53243e8 100644 --- a/lib/internal/test_runner/reporter/rerun.js +++ b/lib/internal/test_runner/reporter/rerun.js @@ -62,6 +62,7 @@ function reportReruns(previousRuns, globalOptions) { name: data.name, children, passed_on_attempt: data.details.passed_on_attempt ?? data.details.attempt, + duration_ms: data.details.duration_ms, }; } } diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index eb888905798..cae309ffc23 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -10,10 +10,12 @@ const { ArrayPrototypeSplice, ArrayPrototypeUnshift, ArrayPrototypeUnshiftApply, + BigInt, Error, FunctionPrototype, MathFloor, MathMax, + MathRound, Number, NumberPrototypeToFixed, ObjectFreeze, @@ -803,14 +805,25 @@ class Test extends AsyncResource { const previousAttempt = this.root.harness.previousRuns[this.attempt - 1]?.[testIdentifier]; if (previousAttempt != null) { this.passedAttempt = previousAttempt.passed_on_attempt; + if (previousAttempt.duration_ms !== undefined) { + this.replayedDurationNs = BigInt(MathRound(previousAttempt.duration_ms * 1_000_000)); + } this.fn = () => { + // Restore the original duration on the synthetic replay. Suites are + // skipped here because Suite.run() unconditionally reassigns + // startTime later; only non-suite tests benefit from setting it. + if (this.reportedType !== 'suite') { + this.startTime = hrtime(); + this.endTime = this.startTime + (this.replayedDurationNs ?? 0n); + } for (let i = 0; i < (previousAttempt.children?.length ?? 0); i++) { const child = previousAttempt.children[i]; const t = this.createSubtest(Test, child.name, { __proto__: null }, noop, { __proto__: null, loc: [child.line, child.column, child.file], }, noop); - t.endTime = t.startTime = hrtime(); + t.startTime = hrtime(); + t.endTime = t.startTime + (t.replayedDurationNs ?? 0n); // For suites, Suite.run() starts the subtests via SafePromiseAll. // Starting them here as well would run them twice, re-invoking the // synthetic children-creator against a now-incremented disambiguator diff --git a/test/fixtures/test-runner/rerun-duration.js b/test/fixtures/test-runner/rerun-duration.js new file mode 100644 index 00000000000..522ca187c52 --- /dev/null +++ b/test/fixtures/test-runner/rerun-duration.js @@ -0,0 +1,13 @@ +'use strict'; +// A passing test with a measurable duration alongside a failing test. The +// failing test forces the rerun feature to retry on a second invocation, +// causing the passing test to be replayed via the synthetic noop stub. +const { test } = require('node:test'); + +test('passing slow test', async () => { + await new Promise((resolve) => setTimeout(resolve, 25)); +}); + +test('always failing', () => { + throw new Error('boom'); +}); diff --git a/test/parallel/test-runner-test-rerun-failures.js b/test/parallel/test-runner-test-rerun-failures.js index 585d5f25f04..f6ae8735e9b 100644 --- a/test/parallel/test-runner-test-rerun-failures.js +++ b/test/parallel/test-runner-test-rerun-failures.js @@ -89,6 +89,7 @@ const getStateFile = async () => { res.forEach((entry) => { for (const item in entry) { delete entry[item].children; + delete entry[item].duration_ms; } }); return res; @@ -152,6 +153,20 @@ test('test should pass on third rerun with `--test`', async () => { assert.deepStrictEqual(await getStateFile(), expectedStateFile); }); +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]; + + await common.spawnPromisified(process.execPath, args); + await common.spawnPromisified(process.execPath, args); + + const raw = JSON.parse(await readFile(stateFile, 'utf8')); + const passKey = Object.keys(raw[0]).find((k) => raw[0][k].name === 'passing slow test'); + assert.ok(passKey, 'expected the passing test to be recorded on attempt 0'); + assert.ok(raw[0][passKey].duration_ms > 0, 'expected a measurable duration on attempt 0'); + assert.strictEqual(raw[1][passKey].duration_ms, raw[0][passKey].duration_ms); +}); + test('using `run` api', async () => { let stream = run({ files: [fixture], rerunFailuresFilePath: stateFile }); stream.on('test:pass', common.mustCall(19));