Skip to content

Commit

Permalink
test_runner: simplify test end time tracking
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cjihrig committed Mar 21, 2024
1 parent f2e7bcc commit 30f6f2c
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ class Test extends AsyncResource {
};

#cancel(error) {
if (this.endTime !== null) {
if (this.endTime !== null || this.error !== null) {
return;
}

Expand Down Expand Up @@ -564,17 +564,15 @@ class Test extends AsyncResource {
return;
}

this.endTime = hrtime();
this.passed = false;
this.error = err;
}

pass() {
if (this.endTime !== null) {
if (this.error !== null) {
return;
}

this.endTime = hrtime();
this.passed = true;
}

Expand Down Expand Up @@ -711,9 +709,6 @@ class Test extends AsyncResource {
await afterEach();
await after();
} catch (err) {
// If one of the after hooks has thrown unset endTime so that the
// catch below can do its cancel/fail logic.
this.endTime = null;
throw err;
}
} catch (err) {
Expand Down Expand Up @@ -761,13 +756,10 @@ class Test extends AsyncResource {
}

postRun(pendingSubtestsError) {
// If the test was cancelled before it started, then the start and end
// times need to be corrected.
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.endTime ??= hrtime();

// The test has run, so recursively cancel any outstanding subtests and
// mark this test as failed if any subtests failed.
Expand Down Expand Up @@ -974,6 +966,7 @@ class TestHook extends Test {
error.failureType = kHookFailure;
}

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

0 comments on commit 30f6f2c

Please sign in to comment.