diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index f228412b7a401d..08d7a9fa393889 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -598,7 +598,7 @@ class Test extends AsyncResource { if (this.parent !== null) { this.parent.activeSubtests++; } - this.startTime = hrtime(); + this.startTime ??= hrtime(); if (this[kShouldAbort]()) { this.postRun(); diff --git a/test/fixtures/test-runner/root-duration.mjs b/test/fixtures/test-runner/root-duration.mjs new file mode 100644 index 00000000000000..b9bdf1d3427304 --- /dev/null +++ b/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); +}); diff --git a/test/parallel/test-runner-root-duration.js b/test/parallel/test-runner-root-duration.js new file mode 100644 index 00000000000000..0c1b69359cc497 --- /dev/null +++ b/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); +});