Skip to content

Commit

Permalink
test_runner: run global after() hook earlier
Browse files Browse the repository at this point in the history
This commit moves the global after() hook execution from the
'beforeExit' event to the point where all tests have finished
running. This gives the global after() a chance to clean up
handles that would otherwise prevent the 'beforeExit' event
from being emitted.

Fixes: #49056
  • Loading branch information
cjihrig committed Aug 7, 2023
1 parent d150316 commit 9b60785
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function setup(root) {
createProcessEventHandler('unhandledRejection', root);
const coverage = configureCoverage(root, globalOptions);
const exitHandler = async () => {
await root.run(new ERR_TEST_FAILURE(
root.postRun(new ERR_TEST_FAILURE(
'Promise resolution is still pending but the event loop has already resolved',
kCancelledByParent));

Expand Down
25 changes: 22 additions & 3 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,16 @@ class Test extends AsyncResource {
}
}

// Clean up the test. Then, try to report the results and execute any
// tests that were pending due to available concurrency.
this.postRun(pendingSubtestsError);
if (this.parent !== null) {
// Clean up the test. Then, try to report the results and execute any
// tests that were pending due to available concurrency.
//
// The root test is skipped here because it is a special case. Its
// postRun() method is called when the process is getting ready to exit.
// This helps catch any asynchronous activity that occurs after the tests
// have finished executing.
this.postRun(pendingSubtestsError);
}
}

postRun(pendingSubtestsError) {
Expand Down Expand Up @@ -687,6 +694,18 @@ class Test extends AsyncResource {
this.parent.addReadySubtest(this);
this.parent.processReadySubtestRange(false);
this.parent.processPendingSubtests();

if (this.parent === this.root &&
this.root.activeSubtests === 0 &&
this.root.pendingSubtests.length === 0 &&
this.root.readySubtests.size === 0 &&
this.root.hooks.after.length > 0) {
// This is done so that any global after() hooks are run. 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.
this.root.run();
}
} else if (!this.reported) {
if (!this.passed && failed === 0 && this.error) {
this.reporter.fail(0, kFilename, this.subtests.length + 1, kFilename, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ not ok 2 - /test/fixtures/test-runner/output/global_after_should_fail_the_test.j
*
*
*
*
...
1..1
# tests 1
Expand Down

0 comments on commit 9b60785

Please sign in to comment.