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.

PR-URL: #52182
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
  • Loading branch information
cjihrig authored and marco-ippolito committed May 3, 2024
1 parent 07e4a42 commit 5cd3df8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
29 changes: 9 additions & 20 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class Test extends AsyncResource {
};

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

Expand Down Expand Up @@ -511,17 +511,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 @@ -654,15 +652,8 @@ class Test extends AsyncResource {
}

this.pass();
try {
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;
}
await afterEach();
await after();
} catch (err) {
if (isTestFailureError(err)) {
if (err.failureType === kTestTimeoutFailure) {
Expand Down Expand Up @@ -698,13 +689,10 @@ 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();
}
// If the test was cancelled before it started, then the start and end
// times need to be corrected.
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.
Expand Down Expand Up @@ -912,6 +900,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
12 changes: 6 additions & 6 deletions test/fixtures/test-runner/output/hooks_spec_reporter.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

describe hooks - no subtests (*ms)
before throws
1 (*ms)
1
'test did not finish before its parent and was cancelled'

2 (*ms)
2
'test did not finish before its parent and was cancelled'

before throws (*ms)
Expand Down Expand Up @@ -390,7 +390,7 @@

- after() called
run after when before throws
1 (*ms)
1
'test did not finish before its parent and was cancelled'

run after when before throws (*ms)
Expand Down Expand Up @@ -422,11 +422,11 @@
failing tests:

*
1 (*ms)
1
'test did not finish before its parent and was cancelled'

*
2 (*ms)
2
'test did not finish before its parent and was cancelled'

*
Expand Down Expand Up @@ -772,7 +772,7 @@
*

*
1 (*ms)
1
'test did not finish before its parent and was cancelled'

*
Expand Down

0 comments on commit 5cd3df8

Please sign in to comment.