Skip to content

Commit 5cd3df8

Browse files
cjihrigmarco-ippolito
authored andcommittedMay 3, 2024
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: #52182 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 07e4a42 commit 5cd3df8

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
@@ -473,7 +473,7 @@ class Test extends AsyncResource {
473473
};
474474

475475
#cancel(error) {
476-
if (this.endTime !== null) {
476+
if (this.endTime !== null || this.error !== null) {
477477
return;
478478
}
479479

@@ -511,17 +511,15 @@ class Test extends AsyncResource {
511511
return;
512512
}
513513

514-
this.endTime = hrtime();
515514
this.passed = false;
516515
this.error = err;
517516
}
518517

519518
pass() {
520-
if (this.endTime !== null) {
519+
if (this.error !== null) {
521520
return;
522521
}
523522

524-
this.endTime = hrtime();
525523
this.passed = true;
526524
}
527525

@@ -654,15 +652,8 @@ class Test extends AsyncResource {
654652
}
655653

656654
this.pass();
657-
try {
658-
await afterEach();
659-
await after();
660-
} catch (err) {
661-
// If one of the after hooks has thrown unset endTime so that the
662-
// catch below can do its cancel/fail logic.
663-
this.endTime = null;
664-
throw err;
665-
}
655+
await afterEach();
656+
await after();
666657
} catch (err) {
667658
if (isTestFailureError(err)) {
668659
if (err.failureType === kTestTimeoutFailure) {
@@ -698,13 +689,10 @@ class Test extends AsyncResource {
698689
}
699690

700691
postRun(pendingSubtestsError) {
701-
this.startTime ??= hrtime();
702-
703-
// If the test was failed before it even started, then the end time will
704-
// be earlier than the start time. Correct that here.
705-
if (this.endTime < this.startTime) {
706-
this.endTime = hrtime();
707-
}
692+
// If the test was cancelled before it started, then the start and end
693+
// times need to be corrected.
694+
this.endTime ??= hrtime();
695+
this.startTime ??= this.endTime;
708696

709697
// The test has run, so recursively cancel any outstanding subtests and
710698
// mark this test as failed if any subtests failed.
@@ -912,6 +900,7 @@ class TestHook extends Test {
912900
error.failureType = kHookFailure;
913901
}
914902

903+
this.endTime ??= hrtime();
915904
parent.reporter.fail(0, loc, parent.subtests.length + 1, loc.file, {
916905
__proto__: null,
917906
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)
Please sign in to comment.