test_runner: avoid overwriting root start time

This commit ensures the root test start time is not overwritten
when top level before()/after() hooks are run.

PR-URL: https://github.com/nodejs/node/pull/52020
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
Colin Ihrig authored and GitHub committed 2024-03-11 03:18:07 +00:00
1 parent 1f193165b9
commit 592c6907bf
3 files changed
+33 -1

No files matched your search

+1 -1
View File
@@ -620,7 +620,7 @@ class Test extends AsyncResource {
if (this.parent !== null) {
this.parent.activeSubtests++;
}
this.startTime = hrtime();
this.startTime ??= hrtime();
if (this[kShouldAbort]()) {
this.postRun();
+7
View File
@@ -0,0 +1,7 @@
import { test, after } from 'node:test';
after(() => {});
test('a test with some delay', (t, done) => {
setTimeout(done, 50);
});
@@ -0,0 +1,25 @@
'use strict';
const { spawnPromisified } = require('../common');
const fixtures = require('../common/fixtures');
const { strictEqual } = require('node:assert');
const { test } = require('node:test');
test('root duration is longer than test duration', async () => {
const {
code,
stderr,
stdout,
} = await spawnPromisified(process.execPath, [
fixtures.path('test-runner/root-duration.mjs'),
]);
strictEqual(code, 0);
strictEqual(stderr, '');
const durations = [...stdout.matchAll(/duration_ms:? ([.\d]+)/g)];
strictEqual(durations.length, 2);
const testDuration = Number.parseFloat(durations[0][1]);
const rootDuration = Number.parseFloat(durations[1][1]);
strictEqual(Number.isNaN(testDuration), false);
strictEqual(Number.isNaN(rootDuration), false);
strictEqual(rootDuration >= testDuration, true);
});