Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: simplify test time tracking #52182

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ class Test extends AsyncResource {
}

if (preventAddingSubtests) {
test.startTime = test.startTime || hrtime();
test.fail(
new ERR_TEST_FAILURE(
'test could not be started because its parent finished',
Expand All @@ -527,7 +526,7 @@ class Test extends AsyncResource {
};

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

Expand All @@ -537,7 +536,6 @@ class Test extends AsyncResource {
kCancelledByParent,
),
);
this.startTime = this.startTime || this.endTime; // If a test was canceled before it was started, e.g inside a hook
this.cancelled = true;
this.abortController.abort();
}
Expand Down Expand Up @@ -566,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 @@ -709,15 +705,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 @@ -763,11 +752,9 @@ class Test extends AsyncResource {
}

postRun(pendingSubtestsError) {
// 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
Expand Down Expand Up @@ -975,6 +962,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