Skip to content

Commit

Permalink
feat: add jestPuppeteer.resetPage (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored and gregberge committed Jan 17, 2019
1 parent cc9bbfa commit 3a5526c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
10 changes: 10 additions & 0 deletions packages/jest-environment-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ it('should put test in debug mode', async () => {
})
```

### `global.jestPuppeteer.resetPage()`

Reset global.page

```js
beforeEach(async () => {
await jestPuppeteer.resetPage()
})
```

### `jest-puppeteer.config.js`

You can specify a `jest-puppeteer.config.js` at the root of the project or define a custom path using `JEST_PUPPETEER_CONFIG` environment variable. It should export a config object or a Promise for a config object.
Expand Down
18 changes: 13 additions & 5 deletions packages/jest-environment-puppeteer/src/PuppeteerEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ class PuppeteerEnvironment extends NodeEnvironment {
)
}

this.global.page = await this.global.context.newPage()
if (config && config.exitOnPageError) {
this.global.page.addListener('pageerror', handleError)
}

this.global.jestPuppeteer = {
debug: async () => {
// eslint-disable-next-line no-eval
Expand Down Expand Up @@ -102,7 +97,20 @@ class PuppeteerEnvironment extends NodeEnvironment {
stdin.on('data', onKeyPress)
})
},
resetPage: async () => {
if (this.global.page) {
this.global.page.removeListener('pageerror', handleError)
await this.global.page.close()
}

this.global.page = await this.global.context.newPage()
if (config && config.exitOnPageError) {
this.global.page.addListener('pageerror', handleError)
}
},
}

await this.global.jestPuppeteer.resetPage()
}

async teardown() {
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-environment-puppeteer/tests/resetPage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('resetPage', () => {
test('should reset page', async () => {
const oldPage = page
await jestPuppeteer.resetPage()
expect(page).not.toBe(oldPage)
expect(page.isClosed()).toBe(false)
expect(oldPage.isClosed()).toBe(true)
})
})

0 comments on commit 3a5526c

Please sign in to comment.