Jest, Puppeteer or ?Nightwatch

After using nightwatch we decided to switch to using jest and puppeteer for the many more apis and functionalities to support our wordpress plugin and chrome extension. A note here though that puppeteer only supports chrome/chromium. If you need to test cross browsers, nightwatch may be better with their browser drivers.

To setup jest and puppeteer. `—save-dev` or `-D` is saving to a dev dependency in package.json.

npm install --save-dev jest-puppeteer puppeteer jest

for simplicity, in package.json, add jest config. Alternatively, jest can have its own jest.config.js

“scripts": { 
“test”:”jest" 
}, 
"jest": { 
"preset": "jest-puppeteer" 
}, 

I’ve also install ndb, a useful debugger for jest and puppeteer

npm install —D ndb 

and in your package.json, scripts can include

“scripts”: { "test:debug": "npx ndb jest” } 

also to add linting for jest

npm install @types/jest 

create a simple sum.js and sum.test.js to test jest https://jestjs.io/docs/en/getting-started

to run the test,

npm run test

create also a simple google.test.js to test puppeteer with jest. `describe` `it` `expect` are jest while `browser` `page` are automatically handled by puppeteer, there is no need to add its dependency here. 

Read jest with puppeteer documents  https://jestjs.io/docs/en/puppeteer

Read puppeteer documents https://github.com/GoogleChrome/puppeteer

describe('Google', () => { 
beforeAll(async () => { 
await page.goto('https://google.com'); 
}); 
it('should be titled "Google"', async () => { 
await expect(page.title()).resolves.toMatch('Google'); 
}); }); 

run

npm run test 

Leave a comment

Your email address will not be published. Required fields are marked *