Skip to content

Commit ae93739

Browse files
UziTechgregberge
authored andcommitted
feat: add jestPuppeteer.resetBrowser method (#237)
1 parent f9d37ef commit ae93739

File tree

4 files changed

+83
-24
lines changed

4 files changed

+83
-24
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,26 @@ it('should put test in debug mode', async () => {
321321
})
322322
```
323323

324+
### `global.jestPuppeteer.resetPage()`
325+
326+
Reset global.page
327+
328+
```js
329+
beforeEach(async () => {
330+
await jestPuppeteer.resetPage()
331+
})
332+
```
333+
334+
### `global.jestPuppeteer.resetBrowser()`
335+
336+
Reset global.browser, global.context, and global.page
337+
338+
```js
339+
beforeEach(async () => {
340+
await jestPuppeteer.resetBrowser()
341+
})
342+
```
343+
324344
### `jest-puppeteer.config.js`
325345

326346
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.

packages/jest-environment-puppeteer/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ beforeEach(async () => {
8787
})
8888
```
8989

90+
### `global.jestPuppeteer.resetBrowser()`
91+
92+
Reset global.browser, global.context, and global.page
93+
94+
```js
95+
beforeEach(async () => {
96+
await jestPuppeteer.resetBrowser()
97+
})
98+
```
99+
90100
### `jest-puppeteer.config.js`
91101

92102
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.

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

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,6 @@ class PuppeteerEnvironment extends NodeEnvironment {
3434
if (!wsEndpoint) {
3535
throw new Error('wsEndpoint not found')
3636
}
37-
this.global.browser = await puppeteer.connect({
38-
...config.connect,
39-
...config.launch,
40-
browserURL: undefined,
41-
browserWSEndpoint: wsEndpoint,
42-
})
43-
44-
if (config.browserContext === 'incognito') {
45-
// Using this, pages will be created in a pristine context.
46-
this.global.context = await this.global.browser.createIncognitoBrowserContext()
47-
} else if (config.browserContext === 'default' || !config.browserContext) {
48-
/**
49-
* Since this is a new browser, browserContexts() will return only one instance
50-
* https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserbrowsercontexts
51-
*/
52-
this.global.context = await this.global.browser.browserContexts()[0]
53-
} else {
54-
throw new Error(
55-
`browserContext should be either 'incognito' or 'default'. Received '${
56-
config.browserContext
57-
}'`,
58-
)
59-
}
6037

6138
this.global.jestPuppeteer = {
6239
debug: async () => {
@@ -109,9 +86,52 @@ class PuppeteerEnvironment extends NodeEnvironment {
10986
this.global.page.addListener('pageerror', handleError)
11087
}
11188
},
89+
resetBrowser: async () => {
90+
if (this.global.page) {
91+
this.global.page.removeListener('pageerror', handleError)
92+
}
93+
if (config.browserContext === 'incognito' && this.global.context) {
94+
await this.global.context.close()
95+
} else if (this.global.page) {
96+
await this.global.page.close()
97+
}
98+
this.global.page = null
99+
100+
if (this.global.browser) {
101+
await this.global.browser.disconnect()
102+
}
103+
104+
this.global.browser = await puppeteer.connect({
105+
...config.connect,
106+
...config.launch,
107+
browserURL: undefined,
108+
browserWSEndpoint: wsEndpoint,
109+
})
110+
111+
if (config.browserContext === 'incognito') {
112+
// Using this, pages will be created in a pristine context.
113+
this.global.context = await this.global.browser.createIncognitoBrowserContext()
114+
} else if (
115+
config.browserContext === 'default' ||
116+
!config.browserContext
117+
) {
118+
/**
119+
* Since this is a new browser, browserContexts() will return only one instance
120+
* https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserbrowsercontexts
121+
*/
122+
this.global.context = await this.global.browser.browserContexts()[0]
123+
} else {
124+
throw new Error(
125+
`browserContext should be either 'incognito' or 'default'. Received '${
126+
config.browserContext
127+
}'`,
128+
)
129+
}
130+
await this.global.jestPuppeteer.resetPage()
131+
},
112132
}
113133

114-
await this.global.jestPuppeteer.resetPage()
134+
await this.global.jestPuppeteer.resetBrowser()
115135
}
116136

117137
async teardown() {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe('resetBrowser', () => {
2+
test('should reset browser', async () => {
3+
const oldBrowser = browser
4+
await jestPuppeteer.resetBrowser()
5+
expect(browser).not.toBe(oldBrowser)
6+
expect(browser.isConnected()).toBe(true)
7+
expect(oldBrowser.isConnected()).toBe(false)
8+
})
9+
})

0 commit comments

Comments
 (0)