-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid running connect in global setup if browserWSEndpoint provi…
…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
Showing
4 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/jest-environment-puppeteer/tests/__fixtures__/browserWsEndpointConfig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
connect: { | ||
browserWSEndpoint: 'wss://end.point', | ||
}, | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/jest-environment-puppeteer/tests/__fixtures__/launchConfig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
launch: { | ||
product: 'chrome', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |