Using with puppeteer
With the Global Setup/Teardown and Async Test Environment APIs, Jest can work smoothly with puppeteer.
Generating code coverage for test files using Puppeteer is currently not possible if your test uses
page.$eval
,page.$$eval
orpage.evaluate
as the passed function is executed outside of Jest's scope. Check out issue #7962 on GitHub for a workaround.
#
Use jest-puppeteer PresetJest Puppeteer provides all required configuration to run your tests using Puppeteer.
- First, install
jest-puppeteer
- Specify preset in your Jest configuration:
- Write your test
There's no need to load any dependencies. Puppeteer's page
and browser
classes will automatically be exposed
See documentation.
#
Custom example without jest-puppeteer presetYou can also hook up puppeteer from scratch. The basic idea is to:
- launch & file the websocket endpoint of puppeteer with Global Setup
- connect to puppeteer from each Test Environment
- close puppeteer with Global Teardown
Here's an example of the GlobalSetup script
Then we need a custom Test Environment for puppeteer
Finally, we can close the puppeteer instance and clean-up the file
With all the things set up, we can now write our tests like this:
Finally, set jest.config.js
to read from these files. (The jest-puppeteer
preset does something like this under the hood.)
Here's the code of full working example.