diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 9c7d95f9420de5..8bf63654bf1f21 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -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, @@ -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() {