From 07e4a42e4b86c82ee31b48f0062d9ad0d5ea8b96 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 21 Mar 2024 13:28:06 -0400 Subject: [PATCH] test_runner: simplify test start time tracking This commit simplifies the logic for tracking test start time. The start time is now set only when a test/suite begins running. If the test/suite never runs, a fallback is provided in postRun(). PR-URL: https://github.com/nodejs/node/pull/52182 Reviewed-By: Yagiz Nizipli Reviewed-By: Chemi Atlow --- lib/internal/test_runner/test.js | 5 ++--- .../test-runner/output/hooks_spec_reporter.snapshot | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index c98a14541650fd..cb4824331e943e 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -454,7 +454,6 @@ class Test extends AsyncResource { } if (preventAddingSubtests) { - test.startTime = test.startTime || hrtime(); test.fail( new ERR_TEST_FAILURE( 'test could not be started because its parent finished', @@ -484,7 +483,6 @@ class Test extends AsyncResource { kCancelledByParent, ), ); - this.startTime = this.startTime || this.endTime; // If a test was canceled before it was started, e.g inside a hook this.cancelled = true; this.abortController.abort(); } @@ -700,12 +698,13 @@ class Test extends AsyncResource { } postRun(pendingSubtestsError) { + this.startTime ??= hrtime(); + // If the test was failed before it even started, then the end time will // be earlier than the start time. Correct that here. if (this.endTime < this.startTime) { this.endTime = hrtime(); } - this.startTime ??= this.endTime; // The test has run, so recursively cancel any outstanding subtests and // mark this test as failed if any subtests failed. diff --git a/test/fixtures/test-runner/output/hooks_spec_reporter.snapshot b/test/fixtures/test-runner/output/hooks_spec_reporter.snapshot index 4158335409aba1..a4f40e4321ff89 100644 --- a/test/fixtures/test-runner/output/hooks_spec_reporter.snapshot +++ b/test/fixtures/test-runner/output/hooks_spec_reporter.snapshot @@ -11,10 +11,10 @@ describe hooks - no subtests (*ms) before throws - 1 + 1 (*ms) 'test did not finish before its parent and was cancelled' - 2 + 2 (*ms) 'test did not finish before its parent and was cancelled' before throws (*ms) @@ -390,7 +390,7 @@ - after() called run after when before throws - 1 + 1 (*ms) 'test did not finish before its parent and was cancelled' run after when before throws (*ms) @@ -422,11 +422,11 @@ failing tests: * - 1 + 1 (*ms) 'test did not finish before its parent and was cancelled' * - 2 + 2 (*ms) 'test did not finish before its parent and was cancelled' * @@ -772,7 +772,7 @@ * * - 1 + 1 (*ms) 'test did not finish before its parent and was cancelled' *