jest-environment-puppeteer
Run your tests using Jest & Puppeteer 🎪✨
npm install jest-environment-puppeteer puppeteer
Usage
Update your Jest configuration:
Use Puppeteer in your tests:
API
global.browser
Give access to the Puppeteer Browser.
global.page
Give access to a Puppeteer Page opened at start (you will use it most of time).
global.context
Give access to a browser context that is instantiated when the browser is launched. You can control whether each test has its own isolated browser context using the browserContext
option in your jest-puppeteer.config.js
.
global.jestPuppeteer.debug()
Put test in debug mode.
- Jest is suspended (no timeout)
- A
debugger
instruction to Chromium, if Puppeteer has been launched with{ devtools: true }
it will stop
global.jestPuppeteer.resetPage()
Reset global.page
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.
launch
<[object]> All Puppeteer launch options can be specified in config. Since it is JavaScript, you can use all stuff you need, including environment.connect
<[object]> All Puppeteer connect options can be specified in config. This is an alternative tolaunch
config, allowing you to connect to an already running instance of Chrome.browserContext
<[string]>. By default, the browser context (cookies, localStorage, etc) is shared between all tests. The following options are available forbrowserContext
: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 totrue
.server
<[Object]> Server options allowed by jest-dev-server
Example 1
// jest-puppeteer.config.jsmoduleexports = launch: dumpio: true headless: processenvHEADLESS !== 'false' server: command: 'node server.js' port: 4444 launchTimeout: 10000 debug: true
Example 2
This example uses an already running instance of Chrome by passing the active web socket endpoint to connect
. This is useful, for example, when you want to connect to Chrome running in the cloud.
// jest-puppeteer.config.jsconst fetch = const dockerHost = 'http://localhost:9222' { const response = await const browserWSEndpoint = await responsewebSocketDebuggerUrl return connect: browserWSEndpoint server: command: 'node server.js' port: 3000 launchTimeout: 10000 debug: true } moduleexports =
Inspiration
Thanks to Fumihiro Xue for his great Jest example.
License
MIT