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

Commit 55570a2

Browse files
authored
Allow configure launch browser type (#193)
1 parent cb17290 commit 55570a2

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

src/PlaywrightEnvironment.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import type {
88
JestPlaywrightJestConfig,
99
SkipOption,
1010
} from './types'
11-
import { CHROMIUM, IMPORT_KIND_PLAYWRIGHT } from './constants'
11+
import {
12+
CHROMIUM,
13+
IMPORT_KIND_PLAYWRIGHT,
14+
LAUNCH,
15+
PERSISTENT,
16+
} from './constants'
1217
import {
1318
getBrowserOptions,
1419
getBrowserType,
@@ -33,22 +38,31 @@ const getBrowserPerProcess = async (
3338
playwrightInstance: GenericBrowser,
3439
browserType: BrowserType,
3540
config: JestPlaywrightConfig,
36-
): Promise<Browser> => {
37-
const { launchOptions, connectOptions } = config
41+
): Promise<Browser | BrowserContext> => {
42+
const { launchType, userDataDir, launchOptions, connectOptions } = config
3843

39-
if (connectOptions) {
40-
const options = getBrowserOptions(browserType, connectOptions)
41-
return playwrightInstance.connect(options)
42-
} else {
44+
if (launchType === LAUNCH || launchType === PERSISTENT) {
4345
// https://github.com/mmarkelov/jest-playwright/issues/42#issuecomment-589170220
4446
if (browserType !== CHROMIUM && launchOptions && launchOptions.args) {
4547
launchOptions.args = launchOptions.args.filter(
4648
(item: string) => item !== '--no-sandbox',
4749
)
4850
}
51+
4952
const options = getBrowserOptions(browserType, launchOptions)
50-
return playwrightInstance.launch(options)
53+
54+
if (launchType === LAUNCH) {
55+
return playwrightInstance.launch(options)
56+
}
57+
58+
if (launchType === PERSISTENT) {
59+
// @ts-ignore
60+
return playwrightInstance.launchPersistentContext(userDataDir!, options)
61+
}
5162
}
63+
64+
const options = getBrowserOptions(browserType, connectOptions)
65+
return playwrightInstance.connect(options)
5266
}
5367

5468
export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
@@ -80,6 +94,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
8094
exitOnPageError,
8195
selectors,
8296
collectCoverage,
97+
launchType,
8398
} = this._jestPlaywrightConfig
8499
let contextOptions = getBrowserOptions(
85100
browserName,
@@ -117,12 +132,22 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
117132
}
118133
this.global.browserName = browserType
119134
this.global.deviceName = deviceName
120-
this.global.browser = await getBrowserPerProcess(
121-
playwrightInstance,
122-
browserType,
123-
this._jestPlaywrightConfig,
124-
)
125-
this.global.context = await this.global.browser.newContext(contextOptions)
135+
this.global.browser =
136+
launchType === PERSISTENT
137+
? null
138+
: await getBrowserPerProcess(
139+
playwrightInstance,
140+
browserType,
141+
this._jestPlaywrightConfig,
142+
)
143+
this.global.context =
144+
launchType === PERSISTENT
145+
? await getBrowserPerProcess(
146+
playwrightInstance,
147+
browserType,
148+
this._jestPlaywrightConfig,
149+
)
150+
: await this.global.browser.newContext(contextOptions)
126151
if (collectCoverage) {
127152
;(this.global.context as BrowserContext).exposeFunction(
128153
'reportCodeCoverage',

src/PlaywrightRunner.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { Config as JestConfig } from '@jest/types'
1212
import type {
1313
BrowserType,
1414
DeviceType,
15+
WsEndpointType,
1516
JestPlaywrightTest,
1617
JestPlaywrightConfig,
1718
} from './types'
@@ -23,14 +24,14 @@ import {
2324
getPlaywrightInstance,
2425
getBrowserOptions,
2526
} from './utils'
26-
import { DEFAULT_TEST_PLAYWRIGHT_TIMEOUT } from './constants'
27+
import { DEFAULT_TEST_PLAYWRIGHT_TIMEOUT, SERVER } from './constants'
2728
import { BrowserServer } from 'playwright-core'
2829
import { setupCoverage, mergeCoverage } from './coverage'
2930

3031
const getBrowserTest = (
3132
test: JestPlaywrightTest,
3233
browser: BrowserType,
33-
wsEndpoint: string,
34+
wsEndpoint: WsEndpointType,
3435
device: DeviceType,
3536
): JestPlaywrightTest => {
3637
const { displayName } = test.context.config
@@ -71,19 +72,22 @@ class PlaywrightRunner extends JestRunner {
7172
}
7273

7374
async getTests(tests: Test[], config: JestPlaywrightConfig): Promise<Test[]> {
74-
const { browsers, devices, launchOptions } = config
75+
const { browsers, devices, launchType, launchOptions } = config
7576
const pwTests: Test[] = []
7677
for (const test of tests) {
7778
for (const browser of browsers) {
7879
checkBrowserEnv(browser)
7980
const { devices: availableDevices, instance } = getPlaywrightInstance(
8081
browser,
8182
)
82-
if (!this.browser2Server[browser]) {
83-
const options = getBrowserOptions(browser, launchOptions)
84-
this.browser2Server[browser] = await instance.launchServer(options)
83+
let wsEndpoint: WsEndpointType = null
84+
if (launchType === SERVER) {
85+
if (!this.browser2Server[browser]) {
86+
const options = getBrowserOptions(browser, launchOptions)
87+
this.browser2Server[browser] = await instance.launchServer(options)
88+
}
89+
wsEndpoint = this.browser2Server[browser]!.wsEndpoint()
8590
}
86-
const wsEndpoint = this.browser2Server[browser]!.wsEndpoint()
8791

8892
if (devices && devices.length) {
8993
devices.forEach((device: DeviceType) => {

src/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ export const CHROMIUM = 'chromium'
66
export const FIREFOX = 'firefox'
77
export const WEBKIT = 'webkit'
88

9+
export const LAUNCH = 'LAUNCH'
10+
export const PERSISTENT = 'PERSISTENT'
11+
export const SERVER = 'SERVER'
12+
913
export const DEFAULT_CONFIG: JestPlaywrightConfig = {
14+
launchType: SERVER,
1015
launchOptions: {},
1116
contextOptions: {},
1217
browsers: [CHROMIUM],
13-
devices: [],
1418
exitOnPageError: true,
1519
collectCoverage: false,
1620
}

src/types.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import type { Config as JestConfig } from '@jest/types'
1111
import type { Context } from 'jest-runner/build/types'
1212
import type { Test } from 'jest-runner'
1313
import type { JestProcessManagerOptions } from 'jest-process-manager'
14-
import { CHROMIUM, FIREFOX, IMPORT_KIND_PLAYWRIGHT, WEBKIT } from './constants'
14+
import {
15+
CHROMIUM,
16+
FIREFOX,
17+
IMPORT_KIND_PLAYWRIGHT,
18+
LAUNCH,
19+
PERSISTENT,
20+
SERVER,
21+
WEBKIT,
22+
} from './constants'
1523

1624
export type BrowserType = typeof CHROMIUM | typeof FIREFOX | typeof WEBKIT
1725

@@ -26,6 +34,8 @@ export type CustomDeviceType = BrowserContextOptions & {
2634

2735
export type DeviceType = CustomDeviceType | string | null
2836

37+
export type WsEndpointType = string | null
38+
2939
export type Packages = Partial<Record<BrowserType, BrowserType>>
3040

3141
export type GenericBrowser = PlaywrightBrowserType<
@@ -45,14 +55,18 @@ export interface Playwright {
4555
devices: typeof devices
4656
}
4757

58+
type LaunchType = typeof LAUNCH | typeof SERVER | typeof PERSISTENT
59+
4860
type Options<T> = T & Partial<Record<BrowserType, T>>
4961

5062
type ConnectOptions = Parameters<GenericBrowser['connect']>[0]
5163

5264
export interface JestPlaywrightConfig {
65+
launchType?: LaunchType
5366
launchOptions?: Options<LaunchOptions>
5467
connectOptions?: Options<ConnectOptions>
5568
contextOptions?: Options<BrowserContextOptions>
69+
userDataDir?: string
5670
exitOnPageError: boolean
5771
browsers: BrowserType[]
5872
devices?: (string | CustomDeviceType)[]
@@ -63,7 +77,7 @@ export interface JestPlaywrightConfig {
6377

6478
export interface JestPlaywrightJestConfig extends JestConfig.ProjectConfig {
6579
browserName: BrowserType
66-
wsEndpoint: string
80+
wsEndpoint: WsEndpointType
6781
device: DeviceType
6882
}
6983

0 commit comments

Comments
 (0)