Jest Puppeteer provides all required configuration to run your tests using Puppeteer.

    • First install jest-puppeteer
    • Specify preset in your Jest configuration:
    1. {
    2. "preset": "jest-puppeteer"
    3. }
    • Write your test

    See .

    Custom example without jest-puppeteer preset

    You 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 TeardownHere's an example of the GlobalSetup script
    1. // setup.js
    2. const puppeteer = require('puppeteer');
    3. const mkdirp = require('mkdirp');
    4. const path = require('path');
    5. const fs = require('fs');
    6. module.exports = async function() {
    7. const browser = await puppeteer.launch();
    8. // store the browser instance so we can teardown it later
    9. // this global is only available in the teardown but not in TestEnvironments
    10. global.__BROWSER_GLOBAL__ = browser;
    11. // use the file system to expose the wsEndpoint for TestEnvironments
    12. mkdirp.sync(DIR);
    13. fs.writeFileSync(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
    14. };

    Finally we can close the puppeteer instance and clean-up the file

    1. // teardown.js
    2. const os = require('os');
    3. const path = require('path');
    4. const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
    5. module.exports = async function() {
    6. // close the browser instance
    7. await global.__BROWSER_GLOBAL__.close();
    8. // clean-up the wsEndpoint file
    9. rimraf.sync(DIR);
    10. };

    With all the things set up, we can now write our tests like this:

    1. module.exports = {
    2. globalSetup: './setup.js',
    3. globalTeardown: './teardown.js',
    4. testEnvironment: './puppeteer_environment.js',
    5. };

    Here's the code of .