Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 936936d

Browse files
authored
Merge pull request #14 from mmarkelov/Ability-to-support-specific-browser
Ability to support specific browser
2 parents 49235b0 + 06d264b commit 936936d

File tree

6 files changed

+143
-20
lines changed

6 files changed

+143
-20
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Running your tests using [Jest](https://github.com/facebook/jest) & [Playwright]
88
npm install jest-playwright-preset playwright
99
```
1010

11+
Also you can use `jest-playwright-preset` with specific playwright packages:
12+
`playwright-webkit`, `playwright-chromium` and `playwright-firefox`
13+
14+
```
15+
npm install jest-playwright-preset playwright-firefox
16+
```
17+
1118
## Usage
1219

1320
Update your Jest configuration:
@@ -47,6 +54,7 @@ You can specify a `jest-playwright.config.js` at the root of the project or defi
4754
## Browser type
4855

4956
You can specify browser in multiple ways:
57+
**Note**: You should do it only if you are using whole playwright package
5058

5159
- With `BROWSER` environment variable
5260
- With your `jest-playwright.config.js`
@@ -56,7 +64,7 @@ If you don't pass any value it will be use `chromium` as default
5664
Use Playwright in your tests:
5765

5866
```json
59-
"test": "BROWSER=chromium jest"
67+
"test": "jest"
6068
```
6169

6270
```js

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"format": "prettier --write \"*.{js,md}\" \"src/*.js\"",
2525
"lint": "eslint .",
2626
"prepublishOnly": "yarn run build",
27-
"test": "jest"
27+
"test:utils": "jest src/utils.test.js",
28+
"test": "npm run test:utils && jest tests"
2829
},
2930
"husky": {
3031
"hooks": {
@@ -38,8 +39,13 @@
3839
"*.js": "eslint --fix"
3940
},
4041
"peerDependencies": {
41-
"jest-environment-node": "^25.1.0",
42-
"playwright": "*"
42+
"jest-environment-node": "^25.1.0"
43+
},
44+
"optionalDependencies": {
45+
"playwright": "*",
46+
"playwright-chromium": "*",
47+
"playwright-firefox": "*",
48+
"playwright-webkit": "*"
4349
},
4450
"devDependencies": {
4551
"@babel/cli": "^7.8.4",

src/PlaywrightEnvironment.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import NodeEnvironment from 'jest-environment-node'
2-
import playwright from 'playwright'
3-
import { checkBrowserEnv, getBrowserType, readConfig } from './utils'
2+
import {
3+
checkBrowserEnv,
4+
getBrowserType,
5+
getPlaywrightInstance,
6+
readConfig,
7+
} from './utils'
48

59
const handleError = error => {
610
process.emit('uncaughtException', error)
@@ -32,13 +36,12 @@ function startBrowserCloseWatchdog() {
3236
}, 50)
3337
}
3438

35-
async function getBrowserPerProcess() {
39+
async function getBrowserPerProcess(playwrightInstance, config) {
3640
if (!browserPerProcess) {
37-
const config = await readConfig()
3841
const browserType = getBrowserType(config)
3942
checkBrowserEnv(browserType)
4043
const { launchBrowserApp } = config
41-
browserPerProcess = await playwright[browserType].launch(launchBrowserApp)
44+
browserPerProcess = await playwrightInstance.launch(launchBrowserApp)
4245
}
4346
return browserPerProcess
4447
}
@@ -58,21 +61,24 @@ class PlaywrightEnvironment extends NodeEnvironment {
5861
async setup() {
5962
resetBrowserCloseWatchdog()
6063
const config = await readConfig()
64+
const browserType = getBrowserType(config)
65+
checkBrowserEnv(browserType)
6166
const { device, context } = config
67+
const playwrightInstance = await getPlaywrightInstance(browserType)
6268
let contextOptions = context
6369

64-
const availableDevices = Object.keys(playwright.devices)
70+
const availableDevices = Object.keys(playwrightInstance.devices)
6571
if (device) {
6672
if (!availableDevices.includes(device)) {
6773
throw new Error(
6874
`Wrong device. Should be one of [${availableDevices}], but got ${device}`,
6975
)
7076
} else {
71-
const { viewport, userAgent } = playwright.devices[device]
77+
const { viewport, userAgent } = playwrightInstance.devices[device]
7278
contextOptions = { ...contextOptions, viewport, userAgent }
7379
}
7480
}
75-
this.global.browser = await getBrowserPerProcess()
81+
this.global.browser = await getBrowserPerProcess(playwrightInstance, config)
7682
this.global.context = await this.global.browser.newContext(contextOptions)
7783
this.global.page = await this.global.context.newPage()
7884
this.global.page.on('pageerror', handleError)

src/utils.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import { CHROMIUM, FIREFOX, WEBKIT, DEFAULT_CONFIG } from './constants'
55

66
const exists = promisify(fs.exists)
77

8+
const checkDependencies = dependencies => {
9+
if (!dependencies) return null
10+
if (dependencies.playwright) return 'playwright'
11+
if (dependencies[`playwright-${CHROMIUM}`]) return `playwright-${CHROMIUM}`
12+
if (dependencies[`playwright-${FIREFOX}`]) return `playwright-${FIREFOX}`
13+
if (dependencies[`playwright-${WEBKIT}`]) return `playwright-${WEBKIT}`
14+
return null
15+
}
16+
817
export function checkBrowserEnv(param) {
918
if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) {
1019
throw new Error(
@@ -21,6 +30,30 @@ export function getBrowserType(config) {
2130
return config.browser || CHROMIUM
2231
}
2332

33+
export async function readPackage() {
34+
const packagePath = 'package.json'
35+
const absConfigPath = path.resolve(process.cwd(), packagePath)
36+
// eslint-disable-next-line global-require,import/no-dynamic-require
37+
const packageConfig = await require(absConfigPath)
38+
const playwright =
39+
checkDependencies(packageConfig.dependencies) ||
40+
checkDependencies(packageConfig.devDependencies)
41+
if (!playwright) {
42+
throw new Error('None of playwright packages was not found in dependencies')
43+
}
44+
return playwright
45+
}
46+
47+
export async function getPlaywrightInstance(browserType) {
48+
const playwrightPackage = await readPackage()
49+
if (playwrightPackage === 'playwright') {
50+
// eslint-disable-next-line global-require, import/no-dynamic-require
51+
return require(playwrightPackage)[browserType]
52+
}
53+
// eslint-disable-next-line global-require, import/no-dynamic-require
54+
return require(playwrightPackage)
55+
}
56+
2457
export async function readConfig() {
2558
const defaultConfig = DEFAULT_CONFIG
2659

src/utils.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { checkBrowserEnv, readPackage } from './utils'
2+
13
const fs = require('fs')
24
const path = require('path')
35
const { readConfig, getBrowserType } = require('./utils')
@@ -80,3 +82,71 @@ describe('getBrowserType', () => {
8082
expect(browserType).toBe(CHROMIUM)
8183
})
8284
})
85+
86+
describe('checkBrowserEnv', () => {
87+
it('should throw Error with unknown type', async () => {
88+
fs.exists.mockImplementationOnce((_, cb) => cb(true))
89+
jest.mock(
90+
path.join(__dirname, '..', 'jest-playwright.config.js'),
91+
() => ({
92+
browser: 'unknown',
93+
}),
94+
{ virtual: true },
95+
)
96+
const config = await readConfig()
97+
const browserType = getBrowserType(config)
98+
expect(() => checkBrowserEnv(browserType)).toThrow()
99+
})
100+
})
101+
102+
describe('readPackage', () => {
103+
it('should return null when dependencies does not passed', async () => {
104+
fs.exists.mockImplementationOnce((_, cb) => cb(true))
105+
jest.mock(
106+
path.join(__dirname, '..', 'package.json'),
107+
() => ({
108+
dependencies: {},
109+
}),
110+
{ virtual: true },
111+
)
112+
let error
113+
try {
114+
await readPackage()
115+
} catch (e) {
116+
error = e
117+
}
118+
expect(error).toEqual(
119+
new Error('None of playwright packages was not found in dependencies'),
120+
)
121+
})
122+
it('should return playwright when it is defined', async () => {
123+
fs.exists.mockImplementationOnce((_, cb) => cb(true))
124+
jest.mock(
125+
path.join(__dirname, '..', 'package.json'),
126+
() => ({
127+
dependencies: {
128+
playwright: '*',
129+
},
130+
}),
131+
{ virtual: true },
132+
)
133+
134+
const playwright = await readPackage()
135+
expect(playwright).toEqual('playwright')
136+
})
137+
it('should return playwright-firefox when it is defined', async () => {
138+
fs.exists.mockImplementationOnce((_, cb) => cb(true))
139+
jest.mock(
140+
path.join(__dirname, '..', 'package.json'),
141+
() => ({
142+
devDependencies: {
143+
'playwright-firefox': '*',
144+
},
145+
}),
146+
{ virtual: true },
147+
)
148+
149+
const playwright = await readPackage()
150+
expect(playwright).toEqual('playwright-firefox')
151+
})
152+
})

0 commit comments

Comments
 (0)