From eea55bd6c633691191ec4a1b437bd3b39a7ea2c9 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 16 Jul 2019 16:26:03 -0700 Subject: [PATCH] fix(testrunner): properly handle testrunner terminations (#4717) This patch improves the logic for test runner termination. With this patch: - TestRunner runs all afterEach/afterAll hooks when a termination happens, properly terminating browser instances - TestRunner cleans up all dangling timeout timers so that node.js process is not retained and is free to exit --- utils/testrunner/TestRunner.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/utils/testrunner/TestRunner.js b/utils/testrunner/TestRunner.js index ef6093dd89d..e104f2a5e14 100644 --- a/utils/testrunner/TestRunner.js +++ b/utils/testrunner/TestRunner.js @@ -35,8 +35,9 @@ class UserCallback { } async run(...args) { + let timeoutId; const timeoutPromise = new Promise(resolve => { - setTimeout(resolve.bind(null, TimeoutError), this.timeout); + timeoutId = setTimeout(resolve.bind(null, TimeoutError), this.timeout); }); try { return await Promise.race([ @@ -46,6 +47,8 @@ class UserCallback { ]); } catch (e) { return e; + } finally { + clearTimeout(timeoutId); } } @@ -192,6 +195,8 @@ class TestPass { return; await this._runHook(workerId, currentSuite, 'beforeAll', state); for (const child of currentSuite.children) { + if (this._termination) + break; if (!this._workerDistribution.hasValue(child, workerId)) continue; if (child instanceof Test) { @@ -234,8 +239,6 @@ class TestPass { } async _runHook(workerId, suite, hookName, ...args) { - if (this._termination) - return; const hook = suite[hookName]; if (!hook) return;