Skip to content

Commit

Permalink
test_runner: avoid overwriting root start time
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig committed Mar 11, 2024
1 parent 1f19316 commit 592c690
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/test_runner/test.js
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/test-runner/root-duration.mjs
@@ -0,0 +1,7 @@
import { test, after } from 'node:test';

after(() => {});

test('a test with some delay', (t, done) => {
setTimeout(done, 50);
});
25 changes: 25 additions & 0 deletions test/parallel/test-runner-root-duration.js
@@ -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);
});

0 comments on commit 592c690

Please sign in to comment.