Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option runBeforeUnloadOnClose #504

Merged
merged 4 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ You can specify a `jest-puppeteer.config.js` at the root of the project or defin
- `default` Each test starts a tab, so all tests share the same context.
- `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._)
- `exitOnPageError` <[boolean]> Exits page on any global error message thrown. Defaults to `true`.
- `runBeforeUnloadOnClose` <[boolean]> Run `page.close()` with `{ runBeforeUnload: true }` when tests finish, see [`page.close`](https://pptr.dev/api/puppeteer.page.close/)
- `server` <[Object]> Server options allowed by [jest-dev-server](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server)

#### Example 1
Expand Down
18 changes: 15 additions & 3 deletions packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ class PuppeteerEnvironment extends NodeEnvironment {
resetPage: async () => {
if (this.global.page) {
this.global.page.removeListener('pageerror', handleError)
await this.global.page.close()
if (this.global.puppeteerConfig.runBeforeUnloadOnClose) {
await this.global.page.close({ runBeforeUnload: true })
} else {
await this.global.page.close()
}
}

this.global.page = await this.global.context.newPage()
Expand All @@ -110,7 +114,11 @@ class PuppeteerEnvironment extends NodeEnvironment {
if (config.browserContext === 'incognito' && this.global.context) {
await this.global.context.close()
} else if (this.global.page) {
await this.global.page.close()
if (this.global.puppeteerConfig.runBeforeUnloadOnClose) {
await this.global.page.close({ runBeforeUnload: true })
} else {
await this.global.page.close()
}
}
this.global.page = null

Expand Down Expand Up @@ -162,7 +170,11 @@ class PuppeteerEnvironment extends NodeEnvironment {
await context.close()
}
} else if (page) {
await page.close()
if (puppeteerConfig.runBeforeUnloadOnClose) {
await page.close({ runBeforeUnload: true })
} else {
await page.close()
}
}

if (browser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
describe('runBeforeUnloadOnClose', () => {
it('shouldn’t call page.close with runBeforeUnload by default', async () => {
const closeSpy = jest.spyOn(page, 'close')
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
await jestPuppeteer.resetPage()
expect(closeSpy).toHaveBeenCalledTimes(1)
expect(closeSpy).toHaveBeenCalledWith()
})

it('should call page.close({ runBeforeUnload: true }) when runBeforeUnloadOnClose is set to true', async () => {
const closeSpy = jest.spyOn(page, 'close')
global.puppeteerConfig.runBeforeUnloadOnClose = true
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
await jestPuppeteer.resetPage()
expect(closeSpy).toHaveBeenCalledTimes(1)
expect(closeSpy).toHaveBeenCalledWith({ runBeforeUnload: true })
})
})