Puppeteer & Jest
Jest-Puppeteer allows you to run tests with Jest on browsers controlled with Puppeteer.
To get started, please install these packages:
npm install --save-dev jest-puppeteer puppeteer jest
Next, specify these settings in your Jest configuration file (jest.config.js):
module.exports = {
rootDir: '.',
testTimeout: 20000,
testMatch: [
'<rootDir>/*.spec.js'
],
// jest-puppeteer refuses connect.browserWSEndpoint with more than one worker
maxWorkers: 1,
preset: 'jest-puppeteer'
}
Configure Jest with Puppeteer
To configure Jest Puppeteer, create a new file called jest-puppeteer.config.js and add this to the file:
module.exports = {
connect: {
browserWSEndpoint: 'wss://cloud.testingbot.com?key=API_KEY&secret=API_SECRET&browserName=chrome&browserVersion=latest'
}
}
This instructs Jest and Puppeteer to connect to the TestingBot browser grid.
Specifying browser and version
To specify on which browser and version your Jest + Puppeteer test should run, you can include both a browserName and browserVersion in the browserWSEndpoint URL.
Run your first test
To create a test, create a new file called sample.spec.js and add this to the file:
describe('TestingBot', () => {
beforeAll(async () => {
await page.goto('https://testingbot.com')
})
it('should have TestingBot in the title', async () => {
expect(await page.title()).toContain('TestingBot')
})
})
Now you can run your first test with Puppeteer on the TestingBot cloud:
jest
This will open testingbot.com in a Chrome browser and verify that the page title contains 'TestingBot'.
You should see something like this in your terminal after running the command:
PASS ./sample.spec.js (6.503s)
TestingBot
✓ should have TestingBot in the title (81ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
This test will also appear in your Member Dashboard.