Skip to content

Commit e02502d

Browse files
authored
Some improvements (#479)
1 parent 6b9c0ee commit e02502d

File tree

8 files changed

+157
-220
lines changed

8 files changed

+157
-220
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ module.exports = {
120120
- `serverOptions` <[object]>. [All `jest-process-manager` options](https://github.com/playwright-community/jest-process-manager#options).
121121
- `selectors` <[array]>. Define [selectors](https://github.com/microsoft/playwright/blob/v0.11.1/docs/api.md#class-selectors). Each selector must be an object with name and script properties.
122122
- `skipInitialization` <[boolean]>. Add you ability to skip first setup `playwright` process. Possible use cases can be found [here](https://github.com/playwright-community/jest-playwright/issues/424)
123-
- `useDefaultBrowserType` <[boolean]>. [Sometimes](https://github.com/microsoft/playwright/issues/2787) `browser` + `device` combinations don't have any sense. With this option tests will be run with [`defaultBrowserType`](https://github.com/microsoft/playwright/pull/3731) of device
123+
- `useDefaultBrowserType` <[boolean]>. [Sometimes](https://github.com/microsoft/playwright/issues/2787) `browser` + `device` combinations don't have any sense. With this option tests will be run with [`defaultBrowserType`](https://github.com/microsoft/playwright/pull/3731) of device. Pay attention that you should define **devices** to correct usage of this option.
124124

125125
### Specific browser options
126126

package-lock.json

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

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,30 @@
4646
"expect-playwright": "^0.2.6",
4747
"jest-circus": "^26.6.3",
4848
"jest-environment-node": "^26.6.2",
49-
"jest-process-manager": "^0.2.6",
49+
"jest-process-manager": "^0.2.7",
5050
"nyc": "^15.1.0",
5151
"playwright-core": ">=1.2.0",
5252
"rimraf": "^3.0.2",
5353
"uuid": "^8.3.1"
5454
},
5555
"devDependencies": {
5656
"@types/debug": "4.1.5",
57-
"@types/jest": "26.0.15",
57+
"@types/jest": "26.0.16",
5858
"@types/jest-dev-server": "4.2.0",
5959
"@types/node": "14.14.10",
6060
"@types/rimraf": "^3.0.0",
6161
"@types/uuid": "^8.3.0",
62-
"@typescript-eslint/eslint-plugin": "4.8.2",
63-
"@typescript-eslint/parser": "4.8.1",
62+
"@typescript-eslint/eslint-plugin": "4.9.0",
63+
"@typescript-eslint/parser": "4.9.0",
6464
"coveralls": "3.1.0",
6565
"eslint": "7.14.0",
6666
"eslint-config-airbnb-base": "14.2.1",
6767
"eslint-config-prettier": "6.15.0",
6868
"eslint-plugin-import": "2.22.1",
69-
"eslint-plugin-prettier": "3.1.4",
69+
"eslint-plugin-prettier": "3.2.0",
7070
"husky": "4.3.0",
7171
"jest": "26.6.3",
72-
"lint-staged": "10.5.2",
72+
"lint-staged": "10.5.3",
7373
"playwright": ">=1.2.0",
7474
"playwright-chromium": ">=1.2.0",
7575
"prettier": "2.2.1",

src/PlaywrightEnvironment.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
241241
resetContext: async (newOptions?: ConnectOptions): Promise<void> => {
242242
const { browser, context } = this.global
243243

244-
if (context) {
245-
await context.close()
246-
}
244+
await context?.close()
247245

248246
let newContextOptions = contextOptions
249247

@@ -258,9 +256,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
258256
resetBrowser: async (newOptions?: ConnectOptions): Promise<void> => {
259257
const { browser } = this.global
260258

261-
if (browser) {
262-
await browser.close()
263-
}
259+
await browser?.close()
264260

265261
this.global.browser = await getBrowserPerProcess(
266262
playwrightInstance as GenericBrowser,
@@ -319,8 +315,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
319315
// Hack to set testTimeout for jestPlaywright debugging
320316
if (
321317
event.name === 'add_test' &&
322-
event.fn &&
323-
event.fn.toString().includes('jestPlaywright.debug()')
318+
event.fn?.toString().includes('jestPlaywright.debug()')
324319
) {
325320
// Set timeout to 4 days
326321
state.testTimeout = 4 * 24 * 60 * 60 * 1000
@@ -330,9 +325,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
330325
async teardown(): Promise<void> {
331326
const { browser, context, page } = this.global
332327
const { collectCoverage } = this._jestPlaywrightConfig
333-
if (page) {
334-
page.removeListener('pageerror', handleError)
335-
}
328+
page?.removeListener('pageerror', handleError)
336329
if (collectCoverage) {
337330
await Promise.all(
338331
(context as BrowserContext).pages().map((p) =>
@@ -345,9 +338,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
345338
await new Promise((resolve) => setTimeout(resolve, 10))
346339
}
347340

348-
if (browser) {
349-
await browser.close()
350-
}
341+
await browser?.close()
351342

352343
await super.teardown()
353344
}

src/PlaywrightRunner.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type { Config as JestConfig } from '@jest/types'
1212
import type {
1313
BrowserType,
1414
BrowserTest,
15-
CustomDeviceType,
1615
DeviceType,
1716
WsEndpointType,
1817
JestPlaywrightTest,
@@ -21,7 +20,7 @@ import type {
2120
} from '../types/global'
2221
import {
2322
checkBrowserEnv,
24-
checkDeviceEnv,
23+
checkDevice,
2524
getDisplayName,
2625
readConfig,
2726
getPlaywrightInstance,
@@ -61,9 +60,7 @@ const getBrowserTest = ({
6160
device,
6261
displayName: {
6362
name: displayName
64-
? `${playwrightDisplayName} ${
65-
typeof displayName === 'string' ? displayName : displayName.name
66-
}`
63+
? `${playwrightDisplayName} ${displayName.name || displayName}`
6764
: playwrightDisplayName,
6865
color: 'yellow',
6966
},
@@ -76,7 +73,7 @@ const getDevices = (
7673
devices: JestPlaywrightConfig['devices'],
7774
availableDevices: typeof PlaywrightDevices,
7875
) => {
79-
let resultDevices: (string | CustomDeviceType)[] = []
76+
let resultDevices: ConfigDeviceType[] = []
8077

8178
if (devices) {
8279
if (devices instanceof RegExp) {
@@ -99,11 +96,7 @@ const getBrowser = (
9996
return availableDevices[device].defaultBrowserType
10097
}
10198

102-
if (device.defaultBrowserType) {
103-
return device.defaultBrowserType
104-
}
105-
106-
return CHROMIUM
99+
return device?.defaultBrowserType || CHROMIUM
107100
}
108101

109102
class PlaywrightRunner extends JestRunner {
@@ -132,7 +125,7 @@ class PlaywrightRunner extends JestRunner {
132125
this.browser2Server[browser] = await instance.launchServer(options)
133126
}
134127
}
135-
return wsEndpoint || this.browser2Server[browser]!.wsEndpoint()
128+
return wsEndpoint || this.browser2Server[browser]?.wsEndpoint() || null
136129
}
137130

138131
async getTests(tests: Test[], config: JestPlaywrightConfig): Promise<Test[]> {
@@ -157,10 +150,7 @@ class PlaywrightRunner extends JestRunner {
157150
(instance as Record<BrowserType, GenericBrowser>)[browser],
158151
)
159152

160-
if (typeof device === 'string') {
161-
const availableDeviceNames = Object.keys(availableDevices)
162-
checkDeviceEnv(device, availableDeviceNames)
163-
}
153+
checkDevice(device, availableDevices)
164154

165155
pwTests.push(
166156
getBrowserTest({ ...browserTest, browser, wsEndpoint, device }),
@@ -190,11 +180,7 @@ class PlaywrightRunner extends JestRunner {
190180

191181
if (resultDevices.length) {
192182
resultDevices.forEach((device: DeviceType) => {
193-
if (typeof device === 'string') {
194-
const availableDeviceNames = Object.keys(availableDevices)
195-
checkDeviceEnv(device, availableDeviceNames)
196-
}
197-
183+
checkDevice(device, availableDevices)
198184
pwTests.push(getBrowserTest({ ...browserTest, device }))
199185
})
200186
} else {

src/utils.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import path from 'path'
33
import * as Utils from './utils'
44
import { DEFAULT_CONFIG, CHROMIUM, FIREFOX } from './constants'
55
import type { BrowserType, JestPlaywrightConfig } from '../types/global'
6+
import { Playwright } from '../types/global'
67

78
const {
89
readConfig,
910
getBrowserType,
1011
getDeviceType,
1112
checkBrowserEnv,
1213
checkDeviceEnv,
14+
checkDevice,
1315
getPlaywrightInstance,
1416
getDisplayName,
1517
getSkipFlag,
@@ -230,6 +232,29 @@ describe('checkDeviceEnv', () => {
230232
})
231233
})
232234

235+
describe('checkDevice', () => {
236+
const devices = {
237+
'iPhone 11': {},
238+
'Pixel 2': {},
239+
'Nexus 4': {},
240+
} as Playwright['devices']
241+
242+
it('should not throw Error if device is exist', async () => {
243+
const device = 'Nexus 4'
244+
expect(() => checkDevice(device, devices)).not.toThrow()
245+
})
246+
247+
it('should not throw Error if device is not string', async () => {
248+
const device = null
249+
expect(() => checkDevice(device, devices)).not.toThrow()
250+
})
251+
252+
it('should throw Error with unknown type', async () => {
253+
const device = 'unknown'
254+
expect(() => checkDevice(device, devices)).toThrow()
255+
})
256+
})
257+
233258
describe('getSkipFlag', () => {
234259
it('should return true if skipOption.browsers includes browserName', async () => {
235260
const skipOptions = { browsers: [CHROMIUM as BrowserType] }

src/utils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import {
2121
const fsPromises = fs.promises
2222

2323
export const checkBrowserEnv = (param: BrowserType): void => {
24-
if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) {
24+
const browsers = [CHROMIUM, FIREFOX, WEBKIT]
25+
if (!browsers.includes(param)) {
2526
throw new Error(
2627
formatError(
27-
`Wrong browser type. Should be one of [${CHROMIUM}, ${FIREFOX}, ${WEBKIT}], but got ${param}`,
28+
`Wrong browser type. Should be one of [${browsers.join(
29+
', ',
30+
)}], but got ${param}`,
2831
),
2932
)
3033
}
@@ -70,6 +73,16 @@ export const checkDeviceEnv = (
7073
}
7174
}
7275

76+
export const checkDevice = (
77+
device: DeviceType,
78+
availableDevices: Playwright['devices'],
79+
): void => {
80+
if (typeof device === 'string') {
81+
const availableDeviceNames = Object.keys(availableDevices)
82+
checkDeviceEnv(device, availableDeviceNames)
83+
}
84+
}
85+
7386
export const getDisplayName = (
7487
browser: BrowserType,
7588
device: DeviceType,
@@ -87,10 +100,7 @@ export const getDisplayName = (
87100

88101
export const getDeviceType = (device: DeviceType): DeviceType => {
89102
const processDevice = process.env.DEVICE
90-
if (processDevice) {
91-
return processDevice
92-
}
93-
return device
103+
return processDevice || device
94104
}
95105

96106
export const getBrowserType = (browser?: BrowserType): BrowserType => {

types/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ declare global {
132132
}
133133
interface Describe {
134134
jestPlaywrightSkip: JestParams<SkipOption>
135+
jestPlaywrightDebug: JestPlaywrightTestDebug
136+
jestPlaywrightConfig: JestPlaywrightTestConfig
135137
}
136138
}
137139
}

0 commit comments

Comments
 (0)