Skip to content

Commit

Permalink
test_runner: cleanup global event listeners after run
Browse files Browse the repository at this point in the history
PR-URL: #53878
Fixes: #53868
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
EddieAbbondanzio authored and marco-ippolito committed Aug 19, 2024
1 parent 2e69e5f commit 367f9e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,13 @@ function setup(root) {
kCancelledByParent));

hook.disable();
process.removeListener('unhandledRejection', rejectionHandler);
process.removeListener('uncaughtException', exceptionHandler);
process.removeListener('unhandledRejection', rejectionHandler);
process.removeListener('beforeExit', exitHandler);
if (globalOptions.isTestRunner) {
process.removeListener('SIGINT', terminationHandler);
process.removeListener('SIGTERM', terminationHandler);
}
};

const terminationHandler = () => {
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ function run(options) {
}

let postRun = () => root.postRun();
let teardown = () => root.harness.teardown();
let filesWatcher;
const opts = {
__proto__: null,
Expand All @@ -604,6 +605,7 @@ function run(options) {
if (watch) {
filesWatcher = watchFiles(testFiles, opts);
postRun = undefined;
teardown = undefined;
}
const runFiles = () => {
root.harness.bootstrapComplete = true;
Expand All @@ -615,7 +617,8 @@ function run(options) {
});
};

PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun);
const setupPromise = PromiseResolve(setup?.(root.reporter));
PromisePrototypeThen(PromisePrototypeThen(PromisePrototypeThen(setupPromise, runFiles), postRun), teardown);

return root.reporter;
}
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,13 @@ describe('forceExit', () => {
});
});
});


// exitHandler doesn't run until after the tests / after hooks finish.
process.on('exit', () => {
assert.strictEqual(process.listeners('uncaughtException').length, 0);
assert.strictEqual(process.listeners('unhandledRejection').length, 0);
assert.strictEqual(process.listeners('beforeExit').length, 0);
assert.strictEqual(process.listeners('SIGINT').length, 0);
assert.strictEqual(process.listeners('SIGTERM').length, 0);
});

0 comments on commit 367f9e7

Please sign in to comment.