Skip to content

Commit

Permalink
feat(jest-environment-puppeteer): Add ability to connect to an alread…
Browse files Browse the repository at this point in the history
…y existing instance of Chrome
  • Loading branch information
gidztech committed Aug 19, 2018
1 parent 89e72b7 commit 9de05f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
26 changes: 22 additions & 4 deletions packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ const handleError = error => {
process.emit('uncaughtException', error)
}

const getBrowser = async config => {
const wsEndpoint = fs.readFileSync(WS_ENDPOINT_PATH, 'utf8')
let slowMo
let ignoreHTTPSErrors

if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}

if (config) {
slowMo = (config.connect && config.connect.slowMo) || (config.launch && config.launch.slowMo)
ignoreHTTPSErrors = (config.connect && config.connect.ignoreHTTPSErrors) || (config.launch && config.launch.ignoreHTTPSErrors)
}

return puppeteer.connect({
slowMo,
ignoreHTTPSErrors,
browserWSEndpoint: wsEndpoint,
})
}

const KEYS = {
CONTROL_C: '\u0003',
CONTROL_D: '\u0004',
Expand All @@ -36,10 +57,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
this.global.browser = await puppeteer.connect({
...config.launch,
browserWSEndpoint: wsEndpoint,
})
this.global.browser = await getBrowser(config)
this.global.page = await this.global.browser.newPage()
if (config && config.exitOnPageError) {
this.global.page.addListener('pageerror', handleError)
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-environment-puppeteer/src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ let browser

export async function setup() {
const config = await readConfig()
browser = await puppeteer.launch(config.launch)
if (config.connect) {
browser = await puppeteer.connect(config.connect)
} else {
browser = await puppeteer.launch(config.launch)
}
mkdirp.sync(DIR)
fs.writeFileSync(WS_ENDPOINT_PATH, browser.wsEndpoint())

Expand Down
8 changes: 7 additions & 1 deletion packages/jest-environment-puppeteer/src/readConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ async function readConfig() {
}

// eslint-disable-next-line global-require, import/no-dynamic-require
const localConfig = require(absConfigPath)
const requiredConfig = require(absConfigPath)
let localConfig
if (typeof requiredConfig === 'function') {
localConfig = await requiredConfig()
} else {
localConfig = requiredConfig
}
return merge({}, defaultConfig, localConfig)
}

Expand Down

0 comments on commit 9de05f0

Please sign in to comment.