Skip to content

Commit

Permalink
test_runner: move end of work check to finalize()
Browse files Browse the repository at this point in the history
This commit moves the end of work check from postRun() to
finalize(). The reasoning is that finalize() is guaranteed to
run in the order that the tests are defined, while postRun() is
not. This makes the check a little simpler.

PR-URL: #52488
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
  • Loading branch information
cjihrig authored and aduh95 committed Apr 29, 2024
1 parent b00766d commit b4ccb6c
Showing 1 changed file with 9 additions and 20 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 @@ -829,29 +829,9 @@ class Test extends AsyncResource {
this.parent.activeSubtests--;
}

// The call to processPendingSubtests() below can change the number of
// pending subtests. When detecting if we are done running tests, we want
// to check if there are no pending subtests both before and after
// calling processPendingSubtests(). Otherwise, it is possible to call
// root.run() multiple times (which is harmless but can trigger an
// EventEmitter leak warning).
const pendingSiblingCount = this.parent.pendingSubtests.length;

this.parent.addReadySubtest(this);
this.parent.processReadySubtestRange(false);
this.parent.processPendingSubtests();

if (this.parent === this.root &&
pendingSiblingCount === 0 &&
this.root.activeSubtests === 0 &&
this.root.pendingSubtests.length === 0 &&
this.root.readySubtests.size === 0) {
// At this point all of the tests have finished running. However, there
// might be ref'ed handles keeping the event loop alive. This gives the
// global after() hook a chance to clean them up. The user may also
// want to force the test runner to exit despite ref'ed handles.
this.root.run();
}
} else if (!this.reported) {
const {
diagnostics,
Expand Down Expand Up @@ -914,6 +894,15 @@ class Test extends AsyncResource {
this.report();
this.parent.waitingOn++;
this.finished = true;

if (this.parent === this.root &&
this.root.waitingOn > this.root.subtests.length) {
// At this point all of the tests have finished running. However, there
// might be ref'ed handles keeping the event loop alive. This gives the
// global after() hook a chance to clean them up. The user may also
// want to force the test runner to exit despite ref'ed handles.
this.root.run();
}
}

duration() {
Expand Down

0 comments on commit b4ccb6c

Please sign in to comment.