Использование Jest с Puppeteer
With the Global Setup/Teardown and Async Test Environment APIs, Jest can work smoothly with puppeteer.
note
Generating code coverage for test files using Puppeteer is currently not possible if your test uses page.$eval, page.$$eval or page.evaluate as the passed function is executed outside of Jest's scope. Check out issue #7962 on GitHub for a workaround.
Использование предустановок jest-puppeteer
Jest Puppeteer provides all required configuration to run your tests using Puppeteer.
- First, install
jest-puppeteer
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev jest-puppeteer
yarn add --dev jest-puppeteer
pnpm add --save-dev jest-puppeteer
bun add --dev jest-puppeteer
- Specify preset in your Jest configuration:
{
"preset": "jest-puppeteer"
}
- Напишите свой тест, например
describe('Google', () => {
beforeAll(async () => {
await page.goto('https://google.com');
});
it('should be titled "Google"', async () => {
await expect(page.title()).resolves.toMatch('Google');
});
});
Нет необходимости загружать зависимости. Puppeteer's page and browser classes will automatically be exposed
See documentation.