Puppeteer tests

Unit tests in Puppeteer are written using Mocha as the test runner and Expect as the assertions library.

Test state

We have some common setup that runs before each test and is defined in mocha-utils.js.

You can use the getTestState function to read state. It exposes the following that you can use in your tests. These will be reset/tidied between tests automatically for you:

If your test needs a browser instance, you can use the setupTestBrowserHooks() function which will automatically configure a browser that will be cleaned between each test suite run. You access this via getTestState().

If your test needs a Puppeteer page and context, you can use the setupTestPageAndContextHooks() function which will configure these. You can access page and context from getTestState() once you have done this.

The best place to look is an existing test to see how they use the helpers.

Skipping tests in specific conditions

To skip tests edit the TestExpectations file. See test runner documentation for more details.

Running tests

npm test
npm run build --workspace=@puppeteer-test/test && npm test

CLI options

Description Option Type
Do not generate coverage report –no-coverage boolean
Do not generate suggestion for updating TestExpectation.json file –no-suggestions boolean
Specify a file to which to save run data –save-stats-to string
Specify a file with a custom Mocha reporter –reporter string
Number of times to retry failed tests. –retries number
Timeout threshold value. –timeout number
Tell Mocha to not run test files in parallel –no-parallel boolean
Generate full stacktrace upon failure –fullTrace boolean
Name of the Test suit defined in TestSuites.json –test-suite string

Helpful information

  ...
  it.only('should work', async function() {
    const {server, page} = await getTestState();
    const response = await page.goto(server.EMPTY_PAGE);
    expect(response.ok).toBe(true);
  });
  ...
  it.skip('should work', async function({server, page}) {
    const {server, page} = await getTestState();
    const response = await page.goto(server.EMPTY_PAGE);
    expect(response.ok).toBe(true);
  });
npm run test:chrome:headful
BINARY=<path-to-executable> npm run test:chrome:headless # Or npm run test:firefox