-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
id: puppeteer | ||
title: Using with puppeteer | ||
--- | ||
|
||
With the | ||
[Global Setup/Teardown](https://facebook.github.io/jest/docs/en/configuration.html#globalsetup-string) | ||
and | ||
[Async Test Environment](https://facebook.github.io/jest/docs/en/configuration.html#testenvironment-string) | ||
APIs, Jest can work smoothly with | ||
[puppeteer](https://github.com/GoogleChrome/puppeteer). | ||
|
||
## A jest-puppeteer example | ||
|
||
The basic idea is to: | ||
|
||
1. launch & file the websocket endpoint of puppeteer with Global Setup | ||
2. connect to puppeteer from each Test Environment | ||
3. close puppeteer with Global Teardown | ||
|
||
Here's an example of the GlobalSetup script | ||
|
||
```js | ||
// setup.js | ||
module.exports = function() { | ||
return new Promise((resolve, reject) => { | ||
puppeteer.launch().then(browser => { | ||
// store the browser instance so we can teardown it later | ||
global.__BROWSER__ = browser; | ||
|
||
// file the wsEndpoint for TestEnvironments | ||
mkdirp.sync(DIR); | ||
fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint()); | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
``` | ||
|
||
Then we need a custom Test Environment for puppeteer | ||
|
||
```js | ||
// puppeteer_environment.js | ||
class PuppeteerEnvironment extends NodeEnvironment { | ||
constructor(config) { | ||
super(config); | ||
} | ||
|
||
async setup() { | ||
await super.setup(); | ||
// get the wsEndpoint | ||
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8'); | ||
if (!wsEndpoint) { | ||
throw new Error('wsEndpoint not found'); | ||
} | ||
|
||
// connect to puppeteer | ||
this.global.__BROWSER__ = await puppeteer.connect({ | ||
browserWSEndpoint: wsEndpoint, | ||
}); | ||
} | ||
|
||
async teardown() { | ||
await super.teardown(); | ||
} | ||
|
||
runScript(script) { | ||
return super.runScript(script); | ||
} | ||
} | ||
``` | ||
|
||
Finally we can close the puppeteer instance and clean-up the file | ||
|
||
```js | ||
// teardown.js | ||
module.exports = function() { | ||
return new Promise((resolve, reject) => { | ||
// close the browser instance | ||
global.__BROWSER__.close(); | ||
|
||
// clean-up the wsEndpoint file | ||
rimraf.sync(DIR); | ||
resolve(); | ||
}); | ||
}; | ||
``` | ||
|
||
With all the things set up, we can now write our tests like this: | ||
|
||
```js | ||
// test.js | ||
describe( | ||
'/ (Home Page)', | ||
() => { | ||
let page; | ||
beforeAll(async () => { | ||
page = await global.__BROWSER__.newPage(); | ||
await page.goto('https://google.com'); | ||
}, timeout); | ||
|
||
it('should load without error', async () => { | ||
let text = await page.evaluate(() => document.body.textContent); | ||
expect(text).toContain('google'); | ||
}); | ||
}, | ||
timeout, | ||
); | ||
``` | ||
|
||
Here's the code of | ||
[full working example](https://github.com/xfumihiro/jest-puppeteer-example). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
"timer-mocks", | ||
"manual-mocks", | ||
"webpack", | ||
"puppeteer", | ||
"migration-guide", | ||
"troubleshooting" | ||
] | ||
|