1
- /* eslint-disable no-console */
1
+ /* eslint-disable no-console, @typescript-eslint/no-unused-vars */
2
2
import type { Event , State } from 'jest-circus'
3
3
import type {
4
4
Browser ,
@@ -8,25 +8,31 @@ import type {
8
8
} from 'playwright-core'
9
9
import type {
10
10
BrowserType ,
11
+ ConfigDeviceType ,
11
12
ConfigParams ,
12
13
ConnectOptions ,
13
14
GenericBrowser ,
14
15
JestPlaywrightConfig ,
15
16
JestPlaywrightProjectConfig ,
17
+ Nullable ,
18
+ Playwright ,
19
+ TestPlaywrightConfigOptions ,
16
20
} from '../types/global'
17
21
import {
18
22
CHROMIUM ,
23
+ CONFIG_ENVIRONMENT_NAME ,
24
+ DEFAULT_CONFIG ,
19
25
FIREFOX ,
20
26
IMPORT_KIND_PLAYWRIGHT ,
21
27
PERSISTENT ,
22
28
LAUNCH ,
23
- CONFIG_ENVIRONMENT_NAME ,
24
29
} from './constants'
25
30
import {
26
31
deepMerge ,
27
32
formatError ,
28
33
getBrowserOptions ,
29
34
getBrowserType ,
35
+ getDeviceBrowserType ,
30
36
getDeviceType ,
31
37
getPlaywrightInstance ,
32
38
} from './utils'
@@ -72,6 +78,36 @@ const getBrowserPerProcess = async (
72
78
return playwrightInstance . connect ( options )
73
79
}
74
80
81
+ const getDeviceConfig = (
82
+ device : Nullable < ConfigDeviceType > | undefined ,
83
+ availableDevices : Playwright [ 'devices' ] ,
84
+ ) : BrowserContextOptions => {
85
+ if ( device ) {
86
+ if ( typeof device === 'string' ) {
87
+ const { defaultBrowserType, ...deviceProps } = availableDevices [ device ]
88
+ return deviceProps
89
+ } else {
90
+ const { name, defaultBrowserType, ...deviceProps } = device
91
+ return deviceProps
92
+ }
93
+ }
94
+ return { }
95
+ }
96
+
97
+ const getDeviceName = (
98
+ device : Nullable < ConfigDeviceType > ,
99
+ ) : Nullable < string > => {
100
+ let deviceName : Nullable < string > = null
101
+ if ( device != null ) {
102
+ if ( typeof device === 'string' ) {
103
+ deviceName = device
104
+ } else {
105
+ deviceName = device . name
106
+ }
107
+ }
108
+ return deviceName
109
+ }
110
+
75
111
export const getPlaywrightEnv = ( basicEnv = 'node' ) : unknown => {
76
112
const RootEnv = require ( basicEnv === 'node'
77
113
? 'jest-environment-node'
@@ -86,6 +122,65 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
86
122
this . _config = config
87
123
}
88
124
125
+ _getSeparateEnvBrowserConfig (
126
+ isDebug : boolean ,
127
+ config : TestPlaywrightConfigOptions ,
128
+ ) : JestPlaywrightConfig {
129
+ const { debugOptions } = this . _jestPlaywrightConfig
130
+ const defaultBrowserConfig : JestPlaywrightConfig = {
131
+ ...DEFAULT_CONFIG ,
132
+ launchType : LAUNCH ,
133
+ }
134
+ let resultBrowserConfig : JestPlaywrightConfig = deepMerge (
135
+ defaultBrowserConfig ,
136
+ config ,
137
+ )
138
+ if ( isDebug ) {
139
+ if ( debugOptions ) {
140
+ resultBrowserConfig = deepMerge ( resultBrowserConfig , debugOptions )
141
+ }
142
+ } else {
143
+ resultBrowserConfig = deepMerge (
144
+ this . _jestPlaywrightConfig ,
145
+ resultBrowserConfig ,
146
+ )
147
+ }
148
+ return resultBrowserConfig
149
+ }
150
+
151
+ _getSeparateEnvContextConfig (
152
+ isDebug : boolean ,
153
+ config : TestPlaywrightConfigOptions ,
154
+ browserName : BrowserType ,
155
+ devices : Playwright [ 'devices' ] ,
156
+ ) : BrowserContextOptions {
157
+ const { device, contextOptions } = config
158
+ const { debugOptions } = this . _jestPlaywrightConfig
159
+ const deviceContextOptions : BrowserContextOptions = getDeviceConfig (
160
+ device ,
161
+ devices ,
162
+ )
163
+ let resultContextOptions : BrowserContextOptions = contextOptions || { }
164
+ if ( isDebug ) {
165
+ if ( debugOptions ?. contextOptions ) {
166
+ resultContextOptions = deepMerge (
167
+ resultContextOptions ,
168
+ debugOptions . contextOptions ! ,
169
+ )
170
+ }
171
+ } else {
172
+ resultContextOptions = deepMerge (
173
+ this . _jestPlaywrightConfig . contextOptions ! ,
174
+ resultContextOptions ,
175
+ )
176
+ }
177
+ resultContextOptions = deepMerge (
178
+ deviceContextOptions ,
179
+ resultContextOptions ,
180
+ )
181
+ return getBrowserOptions ( browserName , resultContextOptions )
182
+ }
183
+
89
184
async setup ( ) : Promise < void > {
90
185
const { wsEndpoint, browserName, testEnvironmentOptions } = this . _config
91
186
this . _jestPlaywrightConfig = testEnvironmentOptions [
@@ -97,7 +192,6 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
97
192
exitOnPageError,
98
193
selectors,
99
194
launchType,
100
- debugOptions,
101
195
skipInitialization,
102
196
} = this . _jestPlaywrightConfig
103
197
if ( wsEndpoint ) {
@@ -112,41 +206,32 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
112
206
this . _jestPlaywrightConfig . contextOptions ,
113
207
)
114
208
const device = getDeviceType ( this . _config . device )
115
- let deviceName : string | null = null
209
+ const deviceName : Nullable < string > = getDeviceName ( device )
116
210
const {
117
211
name,
118
212
instance : playwrightInstance ,
119
213
devices,
120
214
} = getPlaywrightInstance ( browserType )
121
215
122
216
if ( name === IMPORT_KIND_PLAYWRIGHT ) {
123
- // eslint-disable-next-line @typescript-eslint/no-var-requires
124
217
const playwright = require ( 'playwright' )
125
218
if ( selectors ) {
126
219
await Promise . all (
127
- selectors . map ( ( { name, script } ) => {
128
- return playwright . selectors
220
+ selectors . map ( ( { name, script } ) =>
221
+ playwright . selectors
129
222
. register ( name , script )
130
223
. catch ( ( e : Error ) : void => {
131
224
if ( ! e . toString ( ) . includes ( 'has been already' ) ) {
132
225
throw e
133
226
}
134
- } )
135
- } ) ,
227
+ } ) ,
228
+ ) ,
136
229
)
137
230
}
138
231
}
139
232
140
- if ( device != null ) {
141
- if ( typeof device === 'string' ) {
142
- deviceName = device
143
- contextOptions = { ...devices [ device ] , ...contextOptions }
144
- } else {
145
- const { name, ...deviceProps } = device
146
- deviceName = name
147
- contextOptions = { ...deviceProps , ...contextOptions }
148
- }
149
- }
233
+ const deviceBrowserContextOptions = getDeviceConfig ( device , devices )
234
+ contextOptions = deepMerge ( deviceBrowserContextOptions , contextOptions )
150
235
if ( browserType === FIREFOX && contextOptions . isMobile ) {
151
236
console . warn ( formatError ( `isMobile is not supported in ${ FIREFOX } .` ) )
152
237
delete contextOptions . isMobile
@@ -184,37 +269,32 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
184
269
}
185
270
this . global . jestPlaywright = {
186
271
configSeparateEnv : async (
187
- config : JestPlaywrightConfig ,
188
- isDebug ?: boolean ,
272
+ config : TestPlaywrightConfigOptions ,
273
+ isDebug = false ,
189
274
) : Promise < ConfigParams > => {
190
- let resultBrowserConfig : JestPlaywrightConfig
191
- let resultContextOptions : BrowserContextOptions | undefined
192
- if ( isDebug ) {
193
- resultBrowserConfig = debugOptions
194
- ? deepMerge ( config , debugOptions )
195
- : config
196
- resultContextOptions = debugOptions ?. contextOptions
197
- ? deepMerge ( config . contextOptions ! , debugOptions . contextOptions ! )
198
- : config . contextOptions
199
- } else {
200
- resultBrowserConfig = deepMerge ( this . _jestPlaywrightConfig , config )
201
- resultContextOptions = {
202
- ...this . _jestPlaywrightConfig . contextOptions ,
203
- ...config . contextOptions ,
204
- }
205
- }
206
- resultBrowserConfig . launchType = LAUNCH
207
- const browser = await getBrowserPerProcess (
208
- playwrightInstance as GenericBrowser ,
209
- browserType ,
210
- resultBrowserConfig ,
275
+ const { device } = config
276
+ const browserName =
277
+ config . useDefaultBrowserType && device
278
+ ? getDeviceBrowserType ( device , devices )
279
+ : config . browser || browserType
280
+ const resultBrowserConfig : JestPlaywrightConfig = this . _getSeparateEnvBrowserConfig (
281
+ isDebug ,
282
+ config ,
211
283
)
212
- const newContextOptions = getBrowserOptions (
284
+ const resultContextOptions : BrowserContextOptions = this . _getSeparateEnvContextConfig (
285
+ isDebug ,
286
+ config ,
213
287
browserName ,
214
- resultContextOptions ,
288
+ devices ,
289
+ )
290
+ const { instance } = getPlaywrightInstance ( browserName )
291
+ const browser = await getBrowserPerProcess (
292
+ instance as GenericBrowser ,
293
+ browserName ,
294
+ resultBrowserConfig ,
215
295
)
216
296
const context = await ( browser as Browser ) ! . newContext (
217
- newContextOptions ,
297
+ resultContextOptions ,
218
298
)
219
299
const page = await context ! . newPage ( )
220
300
return { browser, context, page }
@@ -239,11 +319,9 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
239
319
240
320
await context ?. close ( )
241
321
242
- let newContextOptions = contextOptions
243
-
244
- if ( newOptions ) {
245
- newContextOptions = { ...newContextOptions , ...newOptions }
246
- }
322
+ const newContextOptions = newOptions
323
+ ? deepMerge ( contextOptions , newOptions )
324
+ : contextOptions
247
325
248
326
this . global . context = await browser . newContext ( newContextOptions )
249
327
@@ -277,11 +355,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
277
355
const { stdin } = process
278
356
const listening = stdin . listenerCount ( 'data' ) > 0
279
357
const onKeyPress = ( key : string ) : void => {
280
- if (
281
- key === KEYS . CONTROL_C ||
282
- key === KEYS . CONTROL_D ||
283
- key === KEYS . ENTER
284
- ) {
358
+ if ( Object . values ( KEYS ) . includes ( key ) ) {
285
359
stdin . removeListener ( 'data' , onKeyPress )
286
360
if ( ! listening ) {
287
361
if ( stdin . isTTY ) {
0 commit comments