Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
cjihrig committed Mar 9, 2024
commit f040bf6c73f1284568d49ade1b8d97b08cb2906e
2 changes: 1 addition & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
});