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

Commit 7ee4523

Browse files
authored
Merge pull request #28 from mmarkelov/Add-ability-to-support-device-from-env
Add ability to support device from env
2 parents 19bde8b + 5a82575 commit 7ee4523

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PlaywrightEnvironment.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import NodeEnvironment from 'jest-environment-node'
22
import {
33
checkBrowserEnv,
4+
checkDeviceEnv,
45
getBrowserType,
6+
getDeviceType,
57
getPlaywrightInstance,
68
readConfig,
79
} from './utils'
@@ -63,20 +65,16 @@ class PlaywrightEnvironment extends NodeEnvironment {
6365
const config = await readConfig()
6466
const browserType = getBrowserType(config)
6567
checkBrowserEnv(browserType)
66-
const { device, context } = config
68+
const { context } = config
69+
const device = getDeviceType(config)
6770
const playwrightInstance = await getPlaywrightInstance(browserType)
6871
let contextOptions = context
6972

7073
const availableDevices = Object.keys(playwrightInstance.devices)
7174
if (device) {
72-
if (!availableDevices.includes(device)) {
73-
throw new Error(
74-
`Wrong device. Should be one of [${availableDevices}], but got ${device}`,
75-
)
76-
} else {
77-
const { viewport, userAgent } = playwrightInstance.devices[device]
78-
contextOptions = { ...contextOptions, viewport, userAgent }
79-
}
75+
checkDeviceEnv(device, availableDevices)
76+
const { viewport, userAgent } = playwrightInstance.devices[device]
77+
contextOptions = { ...contextOptions, viewport, userAgent }
8078
}
8179
this.global.browser = await getBrowserPerProcess(playwrightInstance, config)
8280
this.global.context = await this.global.browser.newContext(contextOptions)

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ export function checkBrowserEnv(param) {
2222
}
2323
}
2424

25+
export function checkDeviceEnv(device, availableDevices) {
26+
if (!availableDevices.includes(device)) {
27+
throw new Error(
28+
`Wrong device. Should be one of [${availableDevices}], but got ${device}`,
29+
)
30+
}
31+
}
32+
33+
export function getDeviceType(config) {
34+
const processDevice = process.env.DEVICE
35+
if (processDevice) {
36+
return processDevice
37+
}
38+
return config.device
39+
}
40+
2541
export function getBrowserType(config) {
2642
const processBrowser = process.env.BROWSER
2743
if (processBrowser) {

src/utils.test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { checkBrowserEnv, readPackage } from './utils'
2-
31
const fs = require('fs')
42
const path = require('path')
5-
const { readConfig, getBrowserType } = require('./utils')
3+
const {
4+
readConfig,
5+
getBrowserType,
6+
getDeviceType,
7+
checkBrowserEnv,
8+
checkDeviceEnv,
9+
readPackage,
10+
} = require('./utils')
611
const { DEFAULT_CONFIG, CHROMIUM } = require('./constants')
712

813
jest.spyOn(fs, 'exists')
@@ -100,6 +105,20 @@ describe('getBrowserType', () => {
100105
})
101106
})
102107

108+
describe('getDeviceType', () => {
109+
it('should return "undefined" when there is no device', async () => {
110+
const config = await readConfig()
111+
const device = getDeviceType(config)
112+
expect(device).toBe(undefined)
113+
})
114+
it('should return BROWSER if defined', async () => {
115+
process.env.DEVICE = 'iPhone 11'
116+
const device = getDeviceType()
117+
expect(device).toBe(process.env.DEVICE)
118+
delete process.env.DEVICE
119+
})
120+
})
121+
103122
describe('checkBrowserEnv', () => {
104123
it('should throw Error with unknown type', async () => {
105124
fs.exists.mockImplementationOnce((_, cb) => cb(true))
@@ -116,6 +135,14 @@ describe('checkBrowserEnv', () => {
116135
})
117136
})
118137

138+
describe('checkDeviceEnv', () => {
139+
it('should throw Error with unknown type', async () => {
140+
const device = 'unknown'
141+
const devices = ['iPhone 11', 'Pixel 2', 'Nexus 4']
142+
expect(() => checkDeviceEnv(device, devices)).toThrow()
143+
})
144+
})
145+
119146
describe('readPackage', () => {
120147
it('should return null when dependencies does not passed', async () => {
121148
fs.exists.mockImplementationOnce((_, cb) => cb(true))

0 commit comments

Comments
 (0)