Skip to content

Commit 317ed12

Browse files
authored
feat: Add option runBeforeUnloadOnClose (#504)
* feat: add option runBeforeUnloadOnClose option that controls page.close({ runBeforeUnload: true }) * chore: add tests for runBeforeUnloadOnClose * chore(readme): document runBeforeUnloadOnClose * chore: rename test for runBeforeUnloadOnClose
1 parent 6b214d4 commit 317ed12

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/jest-environment-puppeteer/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ You can specify a `jest-puppeteer.config.js` at the root of the project or defin
107107
- `default` Each test starts a tab, so all tests share the same context.
108108
- `incognito` Each tests starts an incognito window, so all tests have a separate, isolated context. Useful when running tests that could interfere with one another. (_Example: testing multiple users on the same app at once with login, transactions, etc._)
109109
- `exitOnPageError` <[boolean]> Exits page on any global error message thrown. Defaults to `true`.
110+
- `runBeforeUnloadOnClose` <[boolean]> Run `page.close()` with `{ runBeforeUnload: true }` when tests finish, see [`page.close`](https://pptr.dev/api/puppeteer.page.close/)
110111
- `server` <[Object]> Server options allowed by [jest-dev-server](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server)
111112

112113
#### Example 1

packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ class PuppeteerEnvironment extends NodeEnvironment {
9595
resetPage: async () => {
9696
if (this.global.page) {
9797
this.global.page.removeListener('pageerror', handleError)
98-
await this.global.page.close()
98+
if (this.global.puppeteerConfig.runBeforeUnloadOnClose) {
99+
await this.global.page.close({ runBeforeUnload: true })
100+
} else {
101+
await this.global.page.close()
102+
}
99103
}
100104

101105
this.global.page = await this.global.context.newPage()
@@ -110,7 +114,11 @@ class PuppeteerEnvironment extends NodeEnvironment {
110114
if (config.browserContext === 'incognito' && this.global.context) {
111115
await this.global.context.close()
112116
} else if (this.global.page) {
113-
await this.global.page.close()
117+
if (this.global.puppeteerConfig.runBeforeUnloadOnClose) {
118+
await this.global.page.close({ runBeforeUnload: true })
119+
} else {
120+
await this.global.page.close()
121+
}
114122
}
115123
this.global.page = null
116124

@@ -162,7 +170,11 @@ class PuppeteerEnvironment extends NodeEnvironment {
162170
await context.close()
163171
}
164172
} else if (page) {
165-
await page.close()
173+
if (puppeteerConfig.runBeforeUnloadOnClose) {
174+
await page.close({ runBeforeUnload: true })
175+
} else {
176+
await page.close()
177+
}
166178
}
167179

168180
if (browser) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
describe('runBeforeUnloadOnClose', () => {
2+
it('shouldn’t call page.close with runBeforeUnload by default', async () => {
3+
const closeSpy = jest.spyOn(page, 'close')
4+
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
5+
await jestPuppeteer.resetPage()
6+
expect(closeSpy).toHaveBeenCalledTimes(1)
7+
expect(closeSpy).toHaveBeenCalledWith()
8+
})
9+
10+
it('should call page.close({ runBeforeUnload: true }) when runBeforeUnloadOnClose is set to true', async () => {
11+
const closeSpy = jest.spyOn(page, 'close')
12+
global.puppeteerConfig.runBeforeUnloadOnClose = true
13+
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
14+
await jestPuppeteer.resetPage()
15+
expect(closeSpy).toHaveBeenCalledTimes(1)
16+
expect(closeSpy).toHaveBeenCalledWith({ runBeforeUnload: true })
17+
})
18+
})

0 commit comments

Comments
 (0)