与 puppeteer 一起使用

    provides all required configuration to run your tests using Puppeteer.

    1. First, install jest-puppeteer
    • npm
    • Yarn
    1. yarn add --dev jest-puppeteer
    1. Specify preset in your Jest configuration:
    1. {
    2. "preset": "jest-puppeteer"
    3. }
    1. Write your test

    There’s no need to load any dependencies. There’s no need to load any dependencies. Puppeteer’s page and browser classes will automatically be exposed

    See .

    Custom example without jest-puppeteer preset

    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. };

    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 NodeEnvironment = require('jest-environment-node').default;
    5. const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
    6. class PuppeteerEnvironment extends NodeEnvironment {
    7. constructor(config) {
    8. super(config);
    9. }
    10. async setup() {
    11. await super.setup();
    12. // get the wsEndpoint
    13. const wsEndpoint = await readFile(path.join(DIR, 'wsEndpoint'), 'utf8');
    14. if (!wsEndpoint) {
    15. throw new Error('wsEndpoint not found');
    16. }
    17. // connect to puppeteer
    18. this.global.__BROWSER_GLOBAL__ = await puppeteer.connect({
    19. browserWSEndpoint: wsEndpoint,
    20. });
    21. }
    22. async teardown() {
    23. }
    24. getVmContext() {
    25. return super.getVmContext();
    26. }
    27. module.exports = PuppeteerEnvironment;

    teardown.js

    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.) (The jest-puppeteer preset does something like this under the hood.)

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