diff --git a/test/src/fixtures.spec.ts b/test/src/fixtures.spec.ts index 809f7c32..1e7d0ec0 100644 --- a/test/src/fixtures.spec.ts +++ b/test/src/fixtures.spec.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -/* eslint-disable @typescript-eslint/no-var-requires */ - +import {spawn, execSync} from 'child_process'; import path from 'path'; import expect from 'expect'; @@ -35,7 +34,6 @@ describe('Fixtures', function () { } let dumpioData = ''; - const {spawn} = await import('child_process'); const options = Object.assign({}, defaultBrowserOptions, { pipe: true, dumpio: true, @@ -57,7 +55,6 @@ describe('Fixtures', function () { const {defaultBrowserOptions, puppeteerPath} = await getTestState(); let dumpioData = ''; - const {spawn} = await import('child_process'); const options = Object.assign({}, defaultBrowserOptions, {dumpio: true}); const res = spawn('node', [ path.join(__dirname, '../fixtures', 'dumpio.js'), @@ -76,7 +73,6 @@ describe('Fixtures', function () { const {defaultBrowserOptions, puppeteerPath, puppeteer} = await getTestState(); - const {spawn, execSync} = await import('child_process'); const options = Object.assign({}, defaultBrowserOptions, { // Disable DUMPIO to cleanly read stdout. dumpio: false, @@ -86,31 +82,43 @@ describe('Fixtures', function () { puppeteerPath, JSON.stringify(options), ]); - let wsEndPointCallback: (value: string) => void; - const wsEndPointPromise = new Promise(x => { - wsEndPointCallback = x; - }); - let output = ''; - res.stdout.on('data', data => { - output += data; - if (output.indexOf('\n')) { - wsEndPointCallback(output.substring(0, output.indexOf('\n'))); + let killed = false; + function killProcess() { + if (killed) { + return; } - }); - const browser = await puppeteer.connect({ - browserWSEndpoint: await wsEndPointPromise, - }); - const promises = [ - waitEvent(browser, 'disconnected'), - new Promise(resolve => { - res.on('close', resolve); - }), - ]; - if (process.platform === 'win32') { - execSync(`taskkill /pid ${res.pid} /T /F`); - } else { - process.kill(res.pid!); + if (process.platform === 'win32') { + execSync(`taskkill /pid ${res.pid} /T /F`); + } else { + process.kill(res.pid!); + } + killed = true; + } + try { + let wsEndPointCallback: (value: string) => void; + const wsEndPointPromise = new Promise(x => { + wsEndPointCallback = x; + }); + let output = ''; + res.stdout.on('data', data => { + output += data; + if (output.indexOf('\n')) { + wsEndPointCallback(output.substring(0, output.indexOf('\n'))); + } + }); + const browser = await puppeteer.connect({ + browserWSEndpoint: await wsEndPointPromise, + }); + const promises = [ + waitEvent(browser, 'disconnected'), + new Promise(resolve => { + res.on('close', resolve); + }), + ]; + killProcess(); + await Promise.all(promises); + } finally { + killProcess(); } - await Promise.all(promises); }); });