-
Notifications
You must be signed in to change notification settings - Fork 73
/
PlaywrightEnvironment.ts
305 lines (278 loc) · 9.2 KB
/
PlaywrightEnvironment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* eslint-disable no-console */
import type { Event, State } from 'jest-circus'
import type { Browser, Page, BrowserContext } from 'playwright-core'
import type {
JestPlaywrightConfig,
GenericBrowser,
BrowserType,
JestPlaywrightJestConfig,
SkipOption,
} from './types'
import {
CHROMIUM,
IMPORT_KIND_PLAYWRIGHT,
LAUNCH,
PERSISTENT,
} from './constants'
import {
getBrowserOptions,
getBrowserType,
getDeviceType,
getPlaywrightInstance,
getSkipFlag,
readConfig,
} from './utils'
import { saveCoverageOnPage, saveCoverageToFile } from './coverage'
const handleError = (error: Error): void => {
process.emit('uncaughtException', error)
}
const KEYS = {
CONTROL_C: '\u0003',
CONTROL_D: '\u0004',
ENTER: '\r',
}
const getBrowserPerProcess = async (
playwrightInstance: GenericBrowser,
browserType: BrowserType,
config: JestPlaywrightConfig,
): Promise<Browser | BrowserContext> => {
const { launchType, userDataDir, launchOptions, connectOptions } = config
if (launchType === LAUNCH || launchType === PERSISTENT) {
// https://github.com/mmarkelov/jest-playwright/issues/42#issuecomment-589170220
if (browserType !== CHROMIUM && launchOptions && launchOptions.args) {
launchOptions.args = launchOptions.args.filter(
(item: string) => item !== '--no-sandbox',
)
}
const options = getBrowserOptions(browserType, launchOptions)
if (launchType === LAUNCH) {
return playwrightInstance.launch(options)
}
if (launchType === PERSISTENT) {
// @ts-ignore
return playwrightInstance.launchPersistentContext(userDataDir!, options)
}
}
const options = getBrowserOptions(browserType, connectOptions)
return playwrightInstance.connect(options)
}
export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const RootEnv = require(basicEnv === 'node'
? 'jest-environment-node'
: 'jest-environment-jsdom')
return class PlaywrightEnvironment extends RootEnv {
readonly _config: JestPlaywrightJestConfig
_jestPlaywrightConfig!: JestPlaywrightConfig
constructor(config: JestPlaywrightJestConfig) {
super(config)
this._config = config
}
async setup(): Promise<void> {
const { rootDir, wsEndpoint, browserName } = this._config
this._jestPlaywrightConfig = await readConfig(rootDir)
if (
wsEndpoint &&
!this._jestPlaywrightConfig.connectOptions?.wsEndpoint
) {
this._jestPlaywrightConfig.connectOptions = { wsEndpoint }
}
const browserType = getBrowserType(browserName)
const {
exitOnPageError,
selectors,
collectCoverage,
launchType,
} = this._jestPlaywrightConfig
let contextOptions = getBrowserOptions(
browserName,
this._jestPlaywrightConfig.contextOptions,
)
const device = getDeviceType(this._config.device)
let deviceName: string | null = null
const {
name,
instance: playwrightInstance,
devices,
} = getPlaywrightInstance(browserType)
if (name === IMPORT_KIND_PLAYWRIGHT) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const playwright = require('playwright')
if (selectors) {
await Promise.all(
selectors.map(({ name, script }) => {
return playwright.selectors.register(name, script)
}),
)
}
}
if (device != null) {
if (typeof device === 'string') {
deviceName = device
contextOptions = { ...devices[device], ...contextOptions }
} else {
const { name, ...deviceProps } = device
deviceName = name
contextOptions = { ...deviceProps, ...contextOptions }
}
}
this.global.browserName = browserType
this.global.deviceName = deviceName
this.global.browser =
launchType === PERSISTENT
? null
: await getBrowserPerProcess(
playwrightInstance,
browserType,
this._jestPlaywrightConfig,
)
this.global.context =
launchType === PERSISTENT
? await getBrowserPerProcess(
playwrightInstance,
browserType,
this._jestPlaywrightConfig,
)
: await this.global.browser.newContext(contextOptions)
if (collectCoverage) {
;(this.global.context as BrowserContext).exposeFunction(
'reportCodeCoverage',
saveCoverageToFile,
)
;(this.global.context as BrowserContext).addInitScript(() =>
window.addEventListener('beforeunload', () => {
// @ts-ignore
reportCodeCoverage(window.__coverage__)
}),
)
}
this.global.page = await this.global.context.newPage()
if (exitOnPageError) {
this.global.page.on('pageerror', handleError)
}
this.global.jestPlaywright = {
skip: (skipOption: SkipOption, callback: () => void): void => {
const skipFlag = getSkipFlag(skipOption, browserName, deviceName)
const { describe, it, test } = this.global
if (skipFlag) {
this.global.describe = describe.skip
this.global.it = it.skip
this.global.test = test.skip
}
callback()
this.global.describe = describe
this.global.it = it
this.global.test = test
},
resetPage: async (): Promise<void> => {
const { context, page } = this.global
if (page) {
page.removeListener('pageerror', handleError)
await page.close()
}
this.global.page = await context.newPage()
if (exitOnPageError) {
this.global.page.addListener('pageerror', handleError)
}
},
resetContext: async (): Promise<void> => {
const { browser, context } = this.global
if (context) {
await context.close()
}
this.global.context = await browser.newContext(contextOptions)
await this.global.jestPlaywright.resetPage()
},
resetBrowser: async (): Promise<void> => {
const { browser } = this.global
if (browser) {
await browser.close()
}
this.global.browser = await getBrowserPerProcess(
playwrightInstance,
browserType,
this._jestPlaywrightConfig,
)
this.global.context = await this.global.browser.newContext(
contextOptions,
)
await this.global.jestPlaywright.resetPage()
},
debug: async (): Promise<void> => {
// Run a debugger (in case Playwright has been launched with `{ devtools: true }`)
await this.global.page.evaluate(() => {
// eslint-disable-next-line no-debugger
debugger
})
// eslint-disable-next-line no-console
console.log('\n\n🕵️ Code is paused, press enter to resume')
// Run an infinite promise
return new Promise((resolve) => {
const { stdin } = process
const listening = stdin.listenerCount('data') > 0
const onKeyPress = (key: string): void => {
if (
key === KEYS.CONTROL_C ||
key === KEYS.CONTROL_D ||
key === KEYS.ENTER
) {
stdin.removeListener('data', onKeyPress)
if (!listening) {
if (stdin.isTTY) {
stdin.setRawMode(false)
}
stdin.pause()
}
resolve()
}
}
if (!listening) {
if (stdin.isTTY) {
stdin.setRawMode(true)
}
stdin.resume()
stdin.setEncoding('utf8')
}
stdin.on('data', onKeyPress)
})
},
saveCoverage: async (page: Page): Promise<void> =>
saveCoverageOnPage(page, this._jestPlaywrightConfig.collectCoverage),
}
}
async handleTestEvent(event: Event, state: State): Promise<void> {
// Hack to set testTimeout for jestPlaywright debugging
if (
event.name === 'add_test' &&
event.fn &&
event.fn.toString().includes('jestPlaywright.debug()')
) {
// Set timeout to 4 days
state.testTimeout = 4 * 24 * 60 * 60 * 1000
}
}
async teardown(): Promise<void> {
const { browser, context, page } = this.global
const { collectCoverage } = this._jestPlaywrightConfig
if (page) {
page.removeListener('pageerror', handleError)
}
if (collectCoverage) {
await Promise.all(
(context as BrowserContext).pages().map((p) =>
p.close({
runBeforeUnload: true,
}),
),
)
// wait until coverage data was sent successfully to the exposed function
await new Promise((resolve) => setTimeout(resolve, 10))
}
if (browser) {
await browser.close()
}
await super.teardown()
}
}
}
export default getPlaywrightEnv()