Skip to content

Commit

Permalink
fix: avoid running connect in global setup if browserWSEndpoint provi…
Browse files Browse the repository at this point in the history
…ded 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
  • Loading branch information
Ankit098 authored Dec 13, 2021
1 parent 91ef61a commit c9fa515
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/jest-environment-puppeteer/src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
connect: {
browserWSEndpoint: 'wss://end.point',
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
launch: {
product: 'chrome',
},
}
54 changes: 54 additions & 0 deletions packages/jest-environment-puppeteer/tests/setup.test.js
Original file line number Diff line number Diff line change
@@ -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):(?<num>\d{4,5})(\/devtools\/browser\/)(.*)$/
expect(wsEndPoint).toMatch(wsRegex)
})
})
})

0 comments on commit c9fa515

Please sign in to comment.