Using with puppeteer

With the Global Setup/Teardown and Async Test Environment APIs, Jest can work smoothly with puppeteer.

Using with puppeteer - 图1note

Generating code coverage for test files using Puppeteer is currently not possible if your test uses page.$eval, page.$$eval or page.evaluate as the passed function is executed outside of Jest’s scope. Check out issue #7962 on GitHub for a workaround.

Use jest-puppeteer Preset

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

  1. First, install jest-puppeteer
  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev jest-puppeteer
  1. yarn add --dev jest-puppeteer
  1. pnpm add --save-dev jest-puppeteer
  1. Specify preset in your Jest configuration:
  1. {
  2. "preset": "jest-puppeteer"
  3. }
  1. Write your test
  1. describe('Google', () => {
  2. beforeAll(async () => {
  3. await page.goto('https://google.com');
  4. });
  5. it('should be titled "Google"', async () => {
  6. await expect(page.title()).resolves.toMatch('Google');
  7. });
  8. });

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 preset

You can also hook up puppeteer from scratch. 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

setup.js

  1. const {mkdir, writeFile} = require('fs').promises;
  2. const os = require('os');
  3. const path = require('path');
  4. const puppeteer = require('puppeteer');
  5. const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
  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. globalThis.__BROWSER_GLOBAL__ = browser;
  11. // use the file system to expose the wsEndpoint for TestEnvironments
  12. await mkdir(DIR, {recursive: true});
  13. await writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
  14. };

Then we need a custom Test Environment for puppeteer

puppeteer_environment.js

  1. const {readFile} = require('fs').promises;
  2. const os = require('os');
  3. const path = require('path');
  4. const puppeteer = require('puppeteer');
  5. const NodeEnvironment = require('jest-environment-node').TestEnvironment;
  6. const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
  7. class PuppeteerEnvironment extends NodeEnvironment {
  8. constructor(config) {
  9. super(config);
  10. }
  11. async setup() {
  12. await super.setup();
  13. // get the wsEndpoint
  14. const wsEndpoint = await readFile(path.join(DIR, 'wsEndpoint'), 'utf8');
  15. if (!wsEndpoint) {
  16. throw new Error('wsEndpoint not found');
  17. }
  18. // connect to puppeteer
  19. this.global.__BROWSER_GLOBAL__ = await puppeteer.connect({
  20. browserWSEndpoint: wsEndpoint,
  21. });
  22. }
  23. async teardown() {
  24. if (this.global.__BROWSER_GLOBAL__) {
  25. this.global.__BROWSER_GLOBAL__.disconnect();
  26. }
  27. await super.teardown();
  28. }
  29. getVmContext() {
  30. return super.getVmContext();
  31. }
  32. }
  33. module.exports = PuppeteerEnvironment;

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

teardown.js

  1. const fs = require('fs').promises;
  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 globalThis.__BROWSER_GLOBAL__.close();
  8. // clean-up the wsEndpoint file
  9. await fs.rm(DIR, {recursive: true, force: true});
  10. };

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

test.js

  1. const timeout = 5000;
  2. describe(
  3. '/ (Home Page)',
  4. () => {
  5. let page;
  6. beforeAll(async () => {
  7. page = await globalThis.__BROWSER_GLOBAL__.newPage();
  8. await page.goto('https://google.com');
  9. }, timeout);
  10. it('should load without error', async () => {
  11. const text = await page.evaluate(() => document.body.textContent);
  12. expect(text).toContain('google');
  13. });
  14. },
  15. timeout,
  16. );

Finally, set jest.config.js to read from these files. (The jest-puppeteer preset does something like this under the hood.)

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

Here’s the code of full working example.