Skip to content

Commit 3d59acb

Browse files
cjihrigrdw-msft
authored andcommitted
test_runner: simplify test end time tracking
This commit simplifies the logic for tracking test end time. The end time is now only set in postRun(), which every test runs when it ends. PR-URL: nodejs#52182 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent ba3b2d2 commit 3d59acb

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

lib/internal/test_runner/test.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class Test extends AsyncResource {
526526
};
527527

528528
#cancel(error) {
529-
if (this.endTime !== null) {
529+
if (this.endTime !== null || this.error !== null) {
530530
return;
531531
}
532532

@@ -564,17 +564,15 @@ class Test extends AsyncResource {
564564
return;
565565
}
566566

567-
this.endTime = hrtime();
568567
this.passed = false;
569568
this.error = err;
570569
}
571570

572571
pass() {
573-
if (this.endTime !== null) {
572+
if (this.error !== null) {
574573
return;
575574
}
576575

577-
this.endTime = hrtime();
578576
this.passed = true;
579577
}
580578

@@ -707,15 +705,8 @@ class Test extends AsyncResource {
707705
}
708706

709707
this.pass();
710-
try {
711-
await afterEach();
712-
await after();
713-
} catch (err) {
714-
// If one of the after hooks has thrown unset endTime so that the
715-
// catch below can do its cancel/fail logic.
716-
this.endTime = null;
717-
throw err;
718-
}
708+
await afterEach();
709+
await after();
719710
} catch (err) {
720711
if (isTestFailureError(err)) {
721712
if (err.failureType === kTestTimeoutFailure) {
@@ -761,13 +752,10 @@ class Test extends AsyncResource {
761752
}
762753

763754
postRun(pendingSubtestsError) {
764-
this.startTime ??= hrtime();
765-
766-
// If the test was failed before it even started, then the end time will
767-
// be earlier than the start time. Correct that here.
768-
if (this.endTime < this.startTime) {
769-
this.endTime = hrtime();
770-
}
755+
// If the test was cancelled before it started, then the start and end
756+
// times need to be corrected.
757+
this.endTime ??= hrtime();
758+
this.startTime ??= this.endTime;
771759

772760
// The test has run, so recursively cancel any outstanding subtests and
773761
// mark this test as failed if any subtests failed.
@@ -974,6 +962,7 @@ class TestHook extends Test {
974962
error.failureType = kHookFailure;
975963
}
976964

965+
this.endTime ??= hrtime();
977966
parent.reporter.fail(0, loc, parent.subtests.length + 1, loc.file, {
978967
__proto__: null,
979968
duration_ms: this.duration(),

test/fixtures/test-runner/output/hooks_spec_reporter.snapshot

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
describe hooks - no subtests (*ms)
1313
before throws
14-
1 (*ms)
14+
1
1515
'test did not finish before its parent and was cancelled'
1616

17-
2 (*ms)
17+
2
1818
'test did not finish before its parent and was cancelled'
1919

2020
before throws (*ms)
@@ -390,7 +390,7 @@
390390

391391
- after() called
392392
run after when before throws
393-
1 (*ms)
393+
1
394394
'test did not finish before its parent and was cancelled'
395395

396396
run after when before throws (*ms)
@@ -422,11 +422,11 @@
422422
failing tests:
423423

424424
*
425-
1 (*ms)
425+
1
426426
'test did not finish before its parent and was cancelled'
427427

428428
*
429-
2 (*ms)
429+
2
430430
'test did not finish before its parent and was cancelled'
431431

432432
*
@@ -772,7 +772,7 @@
772772
*
773773

774774
*
775-
1 (*ms)
775+
1
776776
'test did not finish before its parent and was cancelled'
777777

778778
*

0 commit comments

Comments
 (0)