Skip to content

Commit 85a1122

Browse files
aslushnikovxiaoyuhen
authored andcommitted
feat: support "browser" config option (#220)
* feat: support "browser" config option Fixes #171 * add note * fix error message
1 parent 8b64c10 commit 85a1122

File tree

8 files changed

+61
-11
lines changed

8 files changed

+61
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"lerna": "^3.13.2",
4343
"lint-staged": "^8.1.5",
4444
"prettier": "^1.16.4",
45-
"puppeteer": "^1.14.0"
45+
"puppeteer": "^1.14.0",
46+
"puppeteer-firefox": "^0.5.0"
4647
},
4748
"dependencies": {}
4849
}

packages/jest-environment-puppeteer/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ You can specify a `jest-puppeteer.config.js` at the root of the project or defin
9393

9494
- `launch` <[object]> [All Puppeteer launch options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.
9595
- `connect` <[object]> [All Puppeteer connect options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) can be specified in config. This is an alternative to `launch` config, allowing you to connect to an already running instance of Chrome.
96+
- `browser` <[string]>. Define a browser to run tests into.
97+
- `chromium` Each test uses [puppeteer](https://npmjs.com/package/puppeteer) and runs Chromium
98+
- `firefox` Each test uses [puppeteer-firefox](https://npmjs.com/package/puppeteer-firefox) and runs Firefox. This option requires `puppeteer-firefox` as a peer dependency.
9699
- `browserContext` <[string]>. By default, the browser context (cookies, localStorage, etc) is shared between all tests. The following options are available for `browserContext`:
97100
- `default` Each test starts a tab, so all tests share the same context.
98101
- `incognito` Each tests starts an incognito window, so all tests have a separate, isolated context. Useful when running tests that could interfere with one another. (_Example: testing multiple users on the same app at once with login, transactions, etc._)

packages/jest-environment-puppeteer/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
"dev": "yarn build --watch",
2121
"prepublishOnly": "yarn build"
2222
},
23-
"peerDependencies": {
24-
"puppeteer": "^1.5.0"
25-
},
2623
"dependencies": {
2724
"chalk": "^2.4.2",
2825
"cwd": "^0.10.0",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// eslint-disable-next-line
22
import NodeEnvironment from 'jest-environment-node'
3-
import puppeteer from 'puppeteer'
43
import chalk from 'chalk'
5-
import readConfig from './readConfig'
4+
import { readConfig, getPuppeteer } from './readConfig'
65

76
const handleError = error => {
87
process.emit('uncaughtException', error)
@@ -28,6 +27,7 @@ class PuppeteerEnvironment extends NodeEnvironment {
2827

2928
async setup() {
3029
const config = await readConfig()
30+
const puppeteer = getPuppeteer(config)
3131
this.global.puppeteerConfig = config
3232

3333
const wsEndpoint = process.env.PUPPETEER_WS_ENDPOINT

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import {
55
ERROR_TIMEOUT,
66
ERROR_NO_COMMAND,
77
} from 'jest-dev-server'
8-
import puppeteer from 'puppeteer'
98
import chalk from 'chalk'
10-
import readConfig from './readConfig'
9+
import { readConfig, getPuppeteer } from './readConfig'
1110

1211
let browser
1312

1413
export async function setup(jestConfig = {}) {
1514
const config = await readConfig()
15+
const puppeteer = getPuppeteer(config)
1616
if (config.connect) {
1717
browser = await puppeteer.connect(config.connect)
1818
} else {

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const exists = promisify(fs.exists)
88

99
const DEFAULT_CONFIG = {
1010
launch: {},
11+
browser: 'chromium',
1112
browserContext: 'default',
1213
exitOnPageError: true,
1314
}
@@ -23,7 +24,7 @@ const DEFAULT_CONFIG_CI = merge(DEFAULT_CONFIG, {
2324
},
2425
})
2526

26-
async function readConfig() {
27+
export async function readConfig() {
2728
const defaultConfig =
2829
process.env.CI === 'true' ? DEFAULT_CONFIG_CI : DEFAULT_CONFIG
2930

@@ -48,4 +49,17 @@ async function readConfig() {
4849
return merge({}, defaultConfig, localConfig)
4950
}
5051

51-
export default readConfig
52+
export function getPuppeteer(config) {
53+
switch (config.browser.toLowerCase()) {
54+
case 'chromium':
55+
// eslint-disable-next-line global-require, import/no-dynamic-require, import/no-extraneous-dependencies
56+
return require('puppeteer')
57+
case 'firefox':
58+
// eslint-disable-next-line global-require, import/no-dynamic-require, import/no-extraneous-dependencies
59+
return require('puppeteer-firefox')
60+
default:
61+
throw new Error(
62+
`Error: "browser" config option is given an unsupported value: ${browser}`,
63+
)
64+
}
65+
}

packages/jest-environment-puppeteer/tests/readConfig.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import readConfig from '../src/readConfig'
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
import pptrChromium from 'puppeteer'
5+
// eslint-disable-next-line import/no-extraneous-dependencies
6+
import pptrFirefox from 'puppeteer-firefox'
7+
import { readConfig, getPuppeteer } from '../src/readConfig'
48

59
jest.mock('fs')
610

711
function mockExists(value) {
812
fs.exists.mockImplementation((path, callback) => callback(null, value))
913
}
1014

15+
describe('getPuppeteer', () => {
16+
it('should return chromium when specified', async () => {
17+
expect(
18+
getPuppeteer({
19+
browser: 'Chromium',
20+
}),
21+
).toBe(pptrChromium)
22+
})
23+
it('should return firefox when specified', async () => {
24+
expect(
25+
getPuppeteer({
26+
browser: 'Firefox',
27+
}),
28+
).toBe(pptrFirefox)
29+
})
30+
})
31+
1132
describe('readConfig', () => {
1233
describe('with custom config path', () => {
1334
beforeEach(() => {

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7427,6 +7427,20 @@ puppeteer@^1.14.0:
74277427
rimraf "^2.6.1"
74287428
ws "^6.1.0"
74297429

7430+
puppeteer-firefox@^0.5.0:
7431+
version "0.5.0"
7432+
resolved "https://registry.yarnpkg.com/puppeteer-firefox/-/puppeteer-firefox-0.5.0.tgz#5800e48cbbe135adae5fbf3032114064a612d87a"
7433+
integrity sha512-80wl29/Lb0URQ1f77yVXfq+cxCW30Q+1GbpRb32HbzTR9/amOn6D5G99xo8OsDJ6kIfuLyYTLj6HZgwMuDPBBQ==
7434+
dependencies:
7435+
debug "^4.1.0"
7436+
extract-zip "^1.6.6"
7437+
https-proxy-agent "^2.2.1"
7438+
mime "^2.0.3"
7439+
progress "^2.0.1"
7440+
proxy-from-env "^1.0.0"
7441+
rimraf "^2.6.1"
7442+
ws "^6.1.0"
7443+
74307444
q@^1.4.1, q@^1.5.1:
74317445
version "1.5.1"
74327446
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"

0 commit comments

Comments
 (0)