mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
test_runner: preserve run duration when using test-rerun
Signed-off-by: Moshe Atlow <moshe@atlow.co.il> PR-URL: https://github.com/nodejs/node/pull/63429 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Aviv Keller <me@aviv.sh>
This commit is contained in:
1 parent
5a0b32dc24
commit
b1fa59cbb6
4 files changed
+43
-1
No files matched your search
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+13
@@ -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');
|
||||
});
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user