Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid running connect in global setup if browserWSEndpoint provided in config #458

Merged
merged 4 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})