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

Commit 465a2a9

Browse files
mmarkelovmxschmitt
andauthored
Fix devices error with playwright-core (#126)
* Fix devices error with playwright-core * fix: refactored device loading (#127) * Improve some ts in PlaywrightRunner Co-authored-by: Max Schmitt <max@schmitt.mx>
1 parent b660f77 commit 465a2a9

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
'import/no-dynamic-require': 'off',
1515
'import/no-unresolved': 'off',
1616
'prettier/prettier': 'error',
17+
'@typescript-eslint/ban-ts-ignore': 'off',
1718
},
1819
parser: '@typescript-eslint/parser',
1920
parserOptions: {

src/PlaywrightEnvironment.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import type { Config as JestConfig } from '@jest/types'
44
import type { Event, State } from 'jest-circus'
55
import type { Browser } from 'playwright-core'
6-
import playwright from 'playwright-core'
76
import type { Config, GenericBrowser, BrowserType } from './types'
87
import { CHROMIUM, IMPORT_KIND_PLAYWRIGHT } from './constants'
98
import {
@@ -77,14 +76,14 @@ export const getPlaywrightEnv = (basicEnv = 'node') => {
7776
}
7877
//@ts-ignore
7978
const device = getDeviceType(this._config.device)
80-
const playwrightInstance = await getPlaywrightInstance(
79+
const { instance: playwrightInstance, devices } = getPlaywrightInstance(
8180
playwrightPackage,
8281
browserType,
8382
)
8483
let contextOptions = context
8584

8685
if (device) {
87-
const { viewport, userAgent } = playwright.devices[device]
86+
const { viewport, userAgent } = devices[device]
8887
contextOptions = { viewport, userAgent, ...contextOptions }
8988
}
9089
this.global.browserName = browserType

src/PlaywrightRunner.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// @ts-nocheck
21
import JestRunner from 'jest-runner'
3-
import playwright from 'playwright-core'
42
import type {
53
Test,
64
TestRunnerContext,
@@ -17,6 +15,8 @@ import {
1715
checkDeviceEnv,
1816
getDisplayName,
1917
readConfig,
18+
getPlaywrightInstance,
19+
readPackage,
2020
} from './utils'
2121
import { DEFAULT_TEST_PLAYWRIGHT_TIMEOUT } from './constants'
2222

@@ -33,11 +33,14 @@ const getBrowserTest = (
3333
...test.context,
3434
config: {
3535
...test.context.config,
36+
// @ts-ignore
3637
browserName: browser,
3738
device,
3839
displayName: {
3940
name: displayName
40-
? `${playwrightDisplayName} ${displayName.name}`
41+
? `${playwrightDisplayName} ${
42+
typeof displayName === 'string' ? displayName : displayName.name
43+
}`
4144
: playwrightDisplayName,
4245
color: 'yellow',
4346
},
@@ -46,17 +49,22 @@ const getBrowserTest = (
4649
}
4750
}
4851

49-
const getTests = (tests: Test[]): Promise<Test[]> => {
50-
return Promise.all(
52+
const getTests = async (tests: Test[]): Promise<Test[]> => {
53+
const playwrightPackage = await readPackage()
54+
return await Promise.all(
5155
tests.map(async (test) => {
5256
const { rootDir } = test.context.config
5357
const { browsers, devices } = await readConfig(rootDir)
5458
return browsers.flatMap((browser) => {
5559
checkBrowserEnv(browser)
56-
return devices.length
60+
const { devices: availableDevices } = getPlaywrightInstance(
61+
playwrightPackage,
62+
browser,
63+
)
64+
return devices
5765
? devices.flatMap((device) => {
58-
const availableDevices = Object.keys(playwright.devices)
59-
checkDeviceEnv(device, availableDevices)
66+
const availableDeviceNames = Object.keys(availableDevices)
67+
checkDeviceEnv(device, availableDeviceNames)
6068
return getBrowserTest(test, browser, device)
6169
})
6270
: getBrowserTest(test, browser, null)
@@ -87,14 +95,14 @@ class PlaywrightRunner extends JestRunner {
8795
const browserTests = await getTests(tests)
8896

8997
return await (options.serial
90-
? this._createInBandTestRun(
98+
? this['_createInBandTestRun'](
9199
browserTests,
92100
watcher,
93101
onStart,
94102
onResult,
95103
onFailure,
96104
)
97-
: this._createParallelTestRun(
105+
: this['_createParallelTestRun'](
98106
browserTests,
99107
watcher,
100108
onStart,

src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
FirefoxBrowser,
77
BrowserType as PlaywrightBrowserType,
88
BrowserTypeConnectOptions,
9+
DeviceDescriptor,
910
} from 'playwright-core'
1011
import type { JestDevServerOptions } from 'jest-dev-server'
1112
import { CHROMIUM, FIREFOX, IMPORT_KIND_PLAYWRIGHT, WEBKIT } from './constants'
@@ -21,13 +22,20 @@ export type SelectorType = {
2122
name: string
2223
}
2324

25+
type Devices = { [name: string]: DeviceDescriptor }
26+
27+
export interface Playwright {
28+
instance: GenericBrowser
29+
devices: Devices
30+
}
31+
2432
export type PlaywrightRequireType = BrowserType | typeof IMPORT_KIND_PLAYWRIGHT
2533

2634
export interface Config {
2735
launchBrowserApp?: BrowserTypeLaunchOptions
2836
context?: BrowserNewContextOptions
2937
exitOnPageError: boolean
30-
browsers?: BrowserType[]
38+
browsers: BrowserType[]
3139
devices?: string[]
3240
server?: JestDevServerOptions
3341
selectors?: SelectorType[]

src/utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ describe('getPlaywrightInstance', () => {
232232
firefox: 'firefox',
233233
}))
234234

235-
const instance = await getPlaywrightInstance('playwright', 'firefox')
235+
const { instance } = getPlaywrightInstance('playwright', 'firefox')
236236
expect(instance).toEqual('firefox')
237237
})
238238

@@ -243,7 +243,7 @@ describe('getPlaywrightInstance', () => {
243243
chromium: 'chromium',
244244
}))
245245

246-
const instance = await getPlaywrightInstance('chromium', 'chromium')
246+
const { instance } = getPlaywrightInstance('chromium', 'chromium')
247247
expect(instance).toEqual('chromium')
248248
})
249249
})

src/utils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
BrowserType,
66
Config,
77
PlaywrightRequireType,
8-
GenericBrowser,
8+
Playwright,
99
} from './types'
1010
import {
1111
CHROMIUM,
@@ -87,14 +87,22 @@ export const readPackage = async (): Promise<PlaywrightRequireType> => {
8787
return playwright
8888
}
8989

90-
export const getPlaywrightInstance = async (
90+
export const getPlaywrightInstance = (
9191
playwrightPackage: PlaywrightRequireType,
92-
browserType: BrowserType,
93-
): Promise<GenericBrowser> => {
92+
browserName: BrowserType,
93+
): Playwright => {
94+
const buildPlaywrightStructure = (importName: string): Playwright => {
95+
// eslint-disable-next-line @typescript-eslint/no-var-requires
96+
const pw = require(importName)
97+
return {
98+
instance: pw[browserName],
99+
devices: pw['devices'],
100+
}
101+
}
94102
if (playwrightPackage === IMPORT_KIND_PLAYWRIGHT) {
95-
return require('playwright')[browserType]
103+
return buildPlaywrightStructure('playwright')
96104
}
97-
return require(`playwright-${playwrightPackage}`)[playwrightPackage]
105+
return buildPlaywrightStructure(`playwright-${playwrightPackage}`)
98106
}
99107

100108
export const readConfig = async (

0 commit comments

Comments
 (0)