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

Commit 18adb4c

Browse files
authored
Fix reset helper functions (#613)
close #612
1 parent 109d52b commit 18adb4c

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/PlaywrightEnvironment.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
BrowserType,
1111
ConfigDeviceType,
1212
ConfigParams,
13-
ConnectOptions,
1413
GenericBrowser,
1514
JestPlaywrightConfig,
1615
JestPlaywrightProjectConfig,
@@ -198,6 +197,15 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
198197
return getBrowserOptions(browserName, resultContextOptions)
199198
}
200199

200+
async _setNewPageInstance(context = this.global.context) {
201+
const { exitOnPageError } = this._jestPlaywrightConfig
202+
const page = await context.newPage()
203+
if (exitOnPageError) {
204+
page.on('pageerror', handleError)
205+
}
206+
return page
207+
}
208+
201209
async setup(): Promise<void> {
202210
const { wsEndpoint, browserName, testEnvironmentOptions } = this._config
203211
this._jestPlaywrightConfig = testEnvironmentOptions[
@@ -206,7 +214,6 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
206214
const {
207215
connectOptions,
208216
collectCoverage,
209-
exitOnPageError,
210217
selectors,
211218
launchType,
212219
skipInitialization,
@@ -270,10 +277,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
270277
}),
271278
)
272279
}
273-
this.global.page = await this.global.context.newPage()
274-
if (exitOnPageError) {
275-
this.global.page.on('pageerror', handleError)
276-
}
280+
this.global.page = await this._setNewPageInstance()
277281
}
278282
this.global.jestPlaywright = {
279283
configSeparateEnv: async (
@@ -310,36 +314,26 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
310314
return { browserName, deviceName, browser, context, page }
311315
},
312316
resetPage: async (): Promise<void> => {
313-
const { context, page } = this.global
314-
try {
315-
if (page) {
316-
page.removeListener('pageerror', handleError)
317-
await page.close()
318-
}
319-
// eslint-disable-next-line no-empty
320-
} catch (e) {}
321-
322-
this.global.page = await context.newPage()
323-
if (exitOnPageError) {
324-
this.global.page.addListener('pageerror', handleError)
325-
}
317+
await this.global.page?.close()
318+
this.global.page = await this._setNewPageInstance()
326319
},
327-
resetContext: async (newOptions?: ConnectOptions): Promise<void> => {
320+
resetContext: async (
321+
newOptions?: BrowserContextOptions,
322+
): Promise<void> => {
328323
const { browser, context } = this.global
329-
330324
await context?.close()
331325

332326
const newContextOptions = newOptions
333327
? deepMerge(contextOptions, newOptions)
334328
: contextOptions
335329

336330
this.global.context = await browser.newContext(newContextOptions)
337-
338-
await this.global.jestPlaywright.resetPage()
331+
this.global.page = await this._setNewPageInstance()
339332
},
340-
resetBrowser: async (newOptions?: ConnectOptions): Promise<void> => {
333+
resetBrowser: async (
334+
newOptions?: BrowserContextOptions,
335+
): Promise<void> => {
341336
const { browser } = this.global
342-
343337
await browser?.close()
344338

345339
this.global.browser = await getBrowserPerProcess(
@@ -348,9 +342,14 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
348342
this._jestPlaywrightConfig,
349343
)
350344

351-
await this.global.jestPlaywright.resetContext(newOptions)
345+
const newContextOptions = newOptions
346+
? deepMerge(contextOptions, newOptions)
347+
: contextOptions
352348

353-
await this.global.jestPlaywright.resetPage()
349+
this.global.context = await this.global.browser.newContext(
350+
newContextOptions,
351+
)
352+
this.global.page = await this._setNewPageInstance()
354353
},
355354
debug: async (): Promise<void> => {
356355
// Run a debugger (in case Playwright has been launched with `{ devtools: true }`)

0 commit comments

Comments
 (0)