Skip to content

Commit c9fa515

Browse files
authored
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
1 parent 91ef61a commit c9fa515

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

packages/jest-environment-puppeteer/src/global.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ export async function setup(jestConfig = {}) {
2626
config.browserPerWorker && !config.connect ? jestConfig.maxWorkers : 1
2727
process.env.BROWSERS_COUNT = browsersCount
2828

29-
browsers = await Promise.all(
30-
Array.from({ length: browsersCount }).map(() =>
31-
openBrowser(puppeteer, config),
32-
),
33-
)
34-
35-
const wsEndpoints = browsers.map((browser) => browser.wsEndpoint())
29+
let wsEndpoints = []
30+
if(config.connect && config.connect.browserWSEndpoint) {
31+
wsEndpoints = [config.connect.browserWSEndpoint]
32+
} else {
33+
browsers = await Promise.all(
34+
Array.from({ length: browsersCount }).map(() =>
35+
openBrowser(puppeteer, config),
36+
),
37+
)
38+
wsEndpoints = browsers.map((browser) => browser.wsEndpoint())
39+
}
3640

3741
process.env.PUPPETEER_WS_ENDPOINTS = JSON.stringify(wsEndpoints)
3842

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
connect: {
3+
browserWSEndpoint: 'wss://end.point',
4+
},
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
launch: {
3+
product: 'chrome',
4+
},
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import path from 'path'
2+
// eslint-disable-next-line import/no-extraneous-dependencies
3+
import puppeteer from 'puppeteer'
4+
import { setup, teardown } from '../src/global'
5+
6+
describe('setup', () => {
7+
describe('browserWSEndpoint in config connect' , () => {
8+
const connectSpy = jest.spyOn(puppeteer, 'connect')
9+
beforeEach(() => {
10+
process.env.JEST_PUPPETEER_CONFIG = path.resolve(
11+
__dirname,
12+
'__fixtures__/browserWsEndpointConfig.js',
13+
)
14+
})
15+
16+
it('should not call puppeteer.connect', async () => {
17+
await setup()
18+
expect(connectSpy).not.toHaveBeenCalled()
19+
})
20+
21+
it('should set the ws-endpoint to the one provided in config', async () => {
22+
await setup()
23+
expect(process.env.BROWSERS_COUNT).toBe('1')
24+
const wsEndPoint = JSON.parse(process.env.PUPPETEER_WS_ENDPOINTS)[0]
25+
expect(wsEndPoint).toBe('wss://end.point')
26+
})
27+
})
28+
29+
describe('browserWSEndpoint not in config connect' , () => {
30+
const launchSpy = jest.spyOn(puppeteer, 'launch')
31+
beforeEach(() => {
32+
process.env.JEST_PUPPETEER_CONFIG = path.resolve(
33+
__dirname,
34+
'__fixtures__/launchConfig.js',
35+
)
36+
})
37+
afterEach(async () => {
38+
await teardown()
39+
})
40+
41+
it('should call puppeteer.launch or connect as per the need', async () => {
42+
await setup()
43+
expect(launchSpy).toHaveBeenCalled()
44+
})
45+
46+
it('should use ws-endpoint generated by launch or connect', async () => {
47+
await setup()
48+
expect(process.env.BROWSERS_COUNT).toBe('1')
49+
const wsEndPoint = JSON.parse(process.env.PUPPETEER_WS_ENDPOINTS)[0]
50+
const wsRegex = /^(ws):\/\/(127.0.0.1):(?<num>\d{4,5})(\/devtools\/browser\/)(.*)$/
51+
expect(wsEndPoint).toMatch(wsRegex)
52+
})
53+
})
54+
})

0 commit comments

Comments
 (0)