From c00cf45009b263b4427d1d81ca226e3c0d77d305 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Thu, 11 May 2023 15:10:27 +0200 Subject: [PATCH] chore: create a test helper for launching browser (#10152) --- test/.eslintrc.js | 16 ++++++++++++ test/src/mocha-utils.ts | 54 ++++++++++++++++++++++++++++++++++++++++ test/src/network.spec.ts | 12 +++------ 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/test/.eslintrc.js b/test/.eslintrc.js index 9d86da20e14ed..e74e13b8b3736 100644 --- a/test/.eslintrc.js +++ b/test/.eslintrc.js @@ -10,4 +10,20 @@ module.exports = { }, ], }, + overrides: [ + { + files: ['*.spec.ts'], + rules: { + 'no-restricted-syntax': [ + 'warn', + { + message: + 'Use helper command `launch` to make sure the browsers get cleaned', + selector: + 'MemberExpression[object.name="puppeteer"][property.name="launch"]', + }, + ], + }, + }, + ], }; diff --git a/test/src/mocha-utils.ts b/test/src/mocha-utils.ts index 70d9ab505e542..efda1e12f9afb 100644 --- a/test/src/mocha-utils.ts +++ b/test/src/mocha-utils.ts @@ -242,6 +242,10 @@ export const mochaHooks = { ], afterEach: (): void => { + if (browserCleanups.length > 0) { + throw new Error('A manually launched browser was not closed!'); + } + sinon.restore(); }, }; @@ -328,3 +332,53 @@ export const createTimeout = ( }, n); }); }; + +const browserCleanups: Array<() => Promise> = []; + +export const launch = async ( + options: PuppeteerLaunchOptions +): Promise< + PuppeteerTestState & { + close: () => Promise; + } +> => { + const close = async () => { + let cleanup = browserCleanups.pop(); + while (cleanup) { + await cleanup(); + cleanup = browserCleanups.pop(); + } + }; + + try { + const browser = await puppeteer.launch({ + ...defaultBrowserOptions, + ...options, + }); + browserCleanups.push(async () => { + browser.close(); + }); + + const context = await browser.createIncognitoBrowserContext(); + browserCleanups.push(async () => { + context.close(); + }); + + const page = await context.newPage(); + browserCleanups.push(async () => { + page.close(); + }); + + return { + ...getTestState(), + browser, + context, + page, + close, + }; + } catch (error) { + await close(); + + throw error; + } +}; diff --git a/test/src/network.spec.ts b/test/src/network.spec.ts index f3899a5ff7eed..3bbd09b03c834 100644 --- a/test/src/network.spec.ts +++ b/test/src/network.spec.ts @@ -24,6 +24,7 @@ import {HTTPResponse} from 'puppeteer-core/internal/api/HTTPResponse.js'; import { getTestState, + launch, setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; @@ -823,15 +824,9 @@ describe('network', function () { }); it('Cross-origin set-cookie', async () => { - const {httpsServer, puppeteer, defaultBrowserOptions} = getTestState(); - - const browser = await puppeteer.launch({ - ...defaultBrowserOptions, + const {page, httpsServer, close} = await launch({ ignoreHTTPSErrors: true, }); - - const page = await browser.newPage(); - try { await page.goto(httpsServer.PREFIX + '/empty.html'); @@ -855,8 +850,7 @@ describe('network', function () { ]); expect(response.headers()['set-cookie']).toBe(setCookieString); } finally { - await page.close(); - await browser.close(); + await close(); } }); });