Headless browser testing utils via Jest and Puppeteer, based on Jest's example integration.
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:
- Supply the process with a
HOSTNAME
environment variable, where your tests will run (for auth, you may pass inUSER
andPASSWORD
environment variables). - 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);
});
});
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);
});