Skip to content

Commit

Permalink
feat: Add option runBeforeUnloadOnClose (#504)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ayc0 committed Nov 26, 2022
1 parent 6b214d4 commit 317ed12
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
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 })
})
})

0 comments on commit 317ed12

Please sign in to comment.