From c9fa515e51bdcd77ceb5f3aa7ee429a54647df7b Mon Sep 17 00:00:00 2001 From: Ankit Singh <51696887+Ankit098@users.noreply.github.com> Date: Tue, 14 Dec 2021 01:36:23 +0530 Subject: [PATCH] fix: avoid running connect in global setup if browserWSEndpoint provided in config (#458) * fix: avoid running connect in global setup if browserWSEndpoint provided in config * tests: add unit test for avoid extra connect call * chore: fix eslint errors --- .../jest-environment-puppeteer/src/global.js | 18 ++++--- .../__fixtures__/browserWsEndpointConfig.js | 5 ++ .../tests/__fixtures__/launchConfig.js | 5 ++ .../tests/setup.test.js | 54 +++++++++++++++++++ 4 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 packages/jest-environment-puppeteer/tests/__fixtures__/browserWsEndpointConfig.js create mode 100644 packages/jest-environment-puppeteer/tests/__fixtures__/launchConfig.js create mode 100644 packages/jest-environment-puppeteer/tests/setup.test.js diff --git a/packages/jest-environment-puppeteer/src/global.js b/packages/jest-environment-puppeteer/src/global.js index dd7c3c00..f365e0d5 100644 --- a/packages/jest-environment-puppeteer/src/global.js +++ b/packages/jest-environment-puppeteer/src/global.js @@ -26,13 +26,17 @@ export async function setup(jestConfig = {}) { config.browserPerWorker && !config.connect ? jestConfig.maxWorkers : 1 process.env.BROWSERS_COUNT = browsersCount - browsers = await Promise.all( - Array.from({ length: browsersCount }).map(() => - openBrowser(puppeteer, config), - ), - ) - - const wsEndpoints = browsers.map((browser) => browser.wsEndpoint()) + let wsEndpoints = [] + if(config.connect && config.connect.browserWSEndpoint) { + wsEndpoints = [config.connect.browserWSEndpoint] + } else { + browsers = await Promise.all( + Array.from({ length: browsersCount }).map(() => + openBrowser(puppeteer, config), + ), + ) + wsEndpoints = browsers.map((browser) => browser.wsEndpoint()) + } process.env.PUPPETEER_WS_ENDPOINTS = JSON.stringify(wsEndpoints) diff --git a/packages/jest-environment-puppeteer/tests/__fixtures__/browserWsEndpointConfig.js b/packages/jest-environment-puppeteer/tests/__fixtures__/browserWsEndpointConfig.js new file mode 100644 index 00000000..484edc37 --- /dev/null +++ b/packages/jest-environment-puppeteer/tests/__fixtures__/browserWsEndpointConfig.js @@ -0,0 +1,5 @@ +module.exports = { + connect: { + browserWSEndpoint: 'wss://end.point', + }, +} diff --git a/packages/jest-environment-puppeteer/tests/__fixtures__/launchConfig.js b/packages/jest-environment-puppeteer/tests/__fixtures__/launchConfig.js new file mode 100644 index 00000000..61ac78d9 --- /dev/null +++ b/packages/jest-environment-puppeteer/tests/__fixtures__/launchConfig.js @@ -0,0 +1,5 @@ +module.exports = { + launch: { + product: 'chrome', + }, +} diff --git a/packages/jest-environment-puppeteer/tests/setup.test.js b/packages/jest-environment-puppeteer/tests/setup.test.js new file mode 100644 index 00000000..e9f760e3 --- /dev/null +++ b/packages/jest-environment-puppeteer/tests/setup.test.js @@ -0,0 +1,54 @@ +import path from 'path' +// eslint-disable-next-line import/no-extraneous-dependencies +import puppeteer from 'puppeteer' +import { setup, teardown } from '../src/global' + +describe('setup', () => { + describe('browserWSEndpoint in config connect' , () => { + const connectSpy = jest.spyOn(puppeteer, 'connect') + beforeEach(() => { + process.env.JEST_PUPPETEER_CONFIG = path.resolve( + __dirname, + '__fixtures__/browserWsEndpointConfig.js', + ) + }) + + it('should not call puppeteer.connect', async () => { + await setup() + expect(connectSpy).not.toHaveBeenCalled() + }) + + it('should set the ws-endpoint to the one provided in config', async () => { + await setup() + expect(process.env.BROWSERS_COUNT).toBe('1') + const wsEndPoint = JSON.parse(process.env.PUPPETEER_WS_ENDPOINTS)[0] + expect(wsEndPoint).toBe('wss://end.point') + }) + }) + + describe('browserWSEndpoint not in config connect' , () => { + const launchSpy = jest.spyOn(puppeteer, 'launch') + beforeEach(() => { + process.env.JEST_PUPPETEER_CONFIG = path.resolve( + __dirname, + '__fixtures__/launchConfig.js', + ) + }) + afterEach(async () => { + await teardown() + }) + + it('should call puppeteer.launch or connect as per the need', async () => { + await setup() + expect(launchSpy).toHaveBeenCalled() + }) + + it('should use ws-endpoint generated by launch or connect', async () => { + await setup() + expect(process.env.BROWSERS_COUNT).toBe('1') + const wsEndPoint = JSON.parse(process.env.PUPPETEER_WS_ENDPOINTS)[0] + const wsRegex = /^(ws):\/\/(127.0.0.1):(?\d{4,5})(\/devtools\/browser\/)(.*)$/ + expect(wsEndPoint).toMatch(wsRegex) + }) + }) +})