From 592c6907bfe1922f36240e9df076be1864c3d1bd Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sun, 10 Mar 2024 23:18:07 -0400 Subject: [PATCH] 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 Reviewed-By: Chemi Atlow Reviewed-By: Moshe Atlow --- lib/internal/test_runner/test.js | 2 +- test/fixtures/test-runner/root-duration.mjs | 7 ++++++ test/parallel/test-runner-root-duration.js | 25 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/test-runner/root-duration.mjs create mode 100644 test/parallel/test-runner-root-duration.js diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 451032ea801c70..7adf9c9db266e0 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -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(); 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); +});