Skip to content

Commit 590decf

Browse files
cjihrigmarco-ippolito
authored andcommittedMay 2, 2024
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: #52020 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 07322b4 commit 590decf

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
 

‎lib/internal/test_runner/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ class Test extends AsyncResource {
598598
if (this.parent !== null) {
599599
this.parent.activeSubtests++;
600600
}
601-
this.startTime = hrtime();
601+
this.startTime ??= hrtime();
602602

603603
if (this[kShouldAbort]()) {
604604
this.postRun();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test, after } from 'node:test';
2+
3+
after(() => {});
4+
5+
test('a test with some delay', (t, done) => {
6+
setTimeout(done, 50);
7+
});
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
const { spawnPromisified } = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const { strictEqual } = require('node:assert');
5+
const { test } = require('node:test');
6+
7+
test('root duration is longer than test duration', async () => {
8+
const {
9+
code,
10+
stderr,
11+
stdout,
12+
} = await spawnPromisified(process.execPath, [
13+
fixtures.path('test-runner/root-duration.mjs'),
14+
]);
15+
16+
strictEqual(code, 0);
17+
strictEqual(stderr, '');
18+
const durations = [...stdout.matchAll(/duration_ms:? ([.\d]+)/g)];
19+
strictEqual(durations.length, 2);
20+
const testDuration = Number.parseFloat(durations[0][1]);
21+
const rootDuration = Number.parseFloat(durations[1][1]);
22+
strictEqual(Number.isNaN(testDuration), false);
23+
strictEqual(Number.isNaN(rootDuration), false);
24+
strictEqual(rootDuration >= testDuration, true);
25+
});

0 commit comments

Comments
 (0)