Skip to content

Headless browser testing utils via Jest and Puppeteer

Notifications You must be signed in to change notification settings

firstlookmedia/whistleblower

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Whistleblower

Headless browser testing utils via Jest and Puppeteer, based on Jest's example integration.

Usage

Requires Node v7.6.0 or later (upgrade to latest stable version on command line with n stable).

Install with npm install firstlookmedia/whistleblower or yarn add firstlookmedia/whistleblower.

Create a Jest config file that points to Whistleblower's utils. This may also include other Jest config options.

module.exports = {
  globalSetup: 'whistleblower/setup',
  globalTeardown: 'whistleblower/teardown',
  testEnvironment: 'whistleblower/environment',
};

To run tests from the command line:

  1. Supply the process with a HOSTNAME environment variable, where your tests will run (for auth, you may pass in USER and PASSWORD environment variables).
  2. Pass Whistleblower the path of your config file, along with any other Jest CLI options.

For example:

HOSTNAME=www.example.com ./node_modules/.bin/whistleblower --config=path/to/your/config.js
HOSTNAME=www.example.com USER='username' PASSWORD='the password' ./node_modules/.bin/whistleblower --config=path/to/your/config.js

In your test files, access the hostname you provided and a Puppeteer browser instance on the global object, e.g.:

describe('Homepage', () => {
  test('returns 200', async () => {
    const page = await global.__BROWSER__.newPage();
    const response = await page.goto(`https://${global.__HOSTNAME__}`);
    expect(response.status).toBe(200);
  });
});

Common Errors

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

This is a frequent error that occurs when puppeteer takes too long to complete its procedures. Use the second argument in a test block to increase Jest's default timeout value of 5000ms as such:

describe('Issue Page', () => {
  test('returns 200', async () => {
    const page = await global.__BROWSER__.newPage();
    const response = await page.goto(`https://${global.__HOSTNAME__}/${issue.speakingId}`);
    expect(response.status).toBe(200);
  }, 20000);
});

About

Headless browser testing utils via Jest and Puppeteer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published