From 8e1a8d67cec39a89f90b3e659a8c3edec693ea2b Mon Sep 17 00:00:00 2001 From: Ed Abbondanzio Date: Tue, 16 Jul 2024 21:54:40 -0400 Subject: [PATCH 1/3] test_runner: cleanup global event listeners after run --- lib/internal/test_runner/harness.js | 7 ++++++- lib/internal/test_runner/runner.js | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index 4dce0ed2332b1a..a30700a9cf74af 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -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 = () => { diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 6fd1cb72f3b508..2baea71bc3af14 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -566,6 +566,7 @@ function run(options = kEmptyObject) { } let postRun = () => root.postRun(); + let teardown = () => root.harness.teardown(); let filesWatcher; const opts = { __proto__: null, @@ -580,6 +581,7 @@ function run(options = kEmptyObject) { if (watch) { filesWatcher = watchFiles(testFiles, opts); postRun = undefined; + teardown = undefined; } const runFiles = () => { root.harness.bootstrapComplete = true; @@ -591,7 +593,8 @@ function run(options = kEmptyObject) { }); }; - PromisePrototypeThen(PromisePrototypeThen(PromiseResolve(setup?.(root.reporter)), runFiles), postRun); + const setupPromise = PromiseResolve(setup?.(root.reporter)); + PromisePrototypeThen(PromisePrototypeThen(PromisePrototypeThen(setupPromise, runFiles), postRun), teardown); return root.reporter; } From ca09d8779dd53d3718c9e9c3b53ff002fbf87427 Mon Sep 17 00:00:00 2001 From: Ed Abbondanzio Date: Thu, 18 Jul 2024 18:03:21 -0400 Subject: [PATCH 2/3] add test --- test/parallel/test-runner-run.mjs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index 54e882c4ecabe3..5495d202399f5b 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -551,3 +551,9 @@ 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); +}); From c08a7eb00937ce0b1dcd65f23e21e1706db60692 Mon Sep 17 00:00:00 2001 From: Ed Abbondanzio Date: Fri, 19 Jul 2024 20:58:22 -0400 Subject: [PATCH 3/3] add checks for beforeExit, SIGINT, and SIGTERM listeners --- test/parallel/test-runner-run.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index 5495d202399f5b..0b3c0908601004 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -552,8 +552,12 @@ 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); });