Skip to content

Commit f767918

Browse files
cjihrigmarco-ippolito
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: #52182 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent a4f7f4f commit f767918

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

lib/internal/test_runner/test.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class Test extends AsyncResource {
498498
};
499499

500500
#cancel(error) {
501-
if (this.endTime !== null) {
501+
if (this.endTime !== null || this.error !== null) {
502502
return;
503503
}
504504

@@ -536,17 +536,15 @@ class Test extends AsyncResource {
536536
return;
537537
}
538538

539-
this.endTime = hrtime();
540539
this.passed = false;
541540
this.error = err;
542541
}
543542

544543
pass() {
545-
if (this.endTime !== null) {
544+
if (this.error !== null) {
546545
return;
547546
}
548547

549-
this.endTime = hrtime();
550548
this.passed = true;
551549
}
552550

@@ -679,15 +677,8 @@ class Test extends AsyncResource {
679677
}
680678

681679
this.pass();
682-
try {
683-
await afterEach();
684-
await after();
685-
} catch (err) {
686-
// If one of the after hooks has thrown unset endTime so that the
687-
// catch below can do its cancel/fail logic.
688-
this.endTime = null;
689-
throw err;
690-
}
680+
await afterEach();
681+
await after();
691682
} catch (err) {
692683
if (isTestFailureError(err)) {
693684
if (err.failureType === kTestTimeoutFailure) {
@@ -733,13 +724,10 @@ class Test extends AsyncResource {
733724
}
734725

735726
postRun(pendingSubtestsError) {
736-
this.startTime ??= hrtime();
737-
738-
// If the test was failed before it even started, then the end time will
739-
// be earlier than the start time. Correct that here.
740-
if (this.endTime < this.startTime) {
741-
this.endTime = hrtime();
742-
}
727+
// If the test was cancelled before it started, then the start and end
728+
// times need to be corrected.
729+
this.endTime ??= hrtime();
730+
this.startTime ??= this.endTime;
743731

744732
// The test has run, so recursively cancel any outstanding subtests and
745733
// mark this test as failed if any subtests failed.
@@ -946,6 +934,7 @@ class TestHook extends Test {
946934
error.failureType = kHookFailure;
947935
}
948936

937+
this.endTime ??= hrtime();
949938
parent.reporter.fail(0, loc, parent.subtests.length + 1, loc.file, {
950939
__proto__: null,
951940
duration_ms: this.duration(),

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

Lines changed: 6 additions & 6 deletions
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)