Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a max timeout for Jest test running Puppeteer?

Tried looking through the docs, but didn't find a way to set a max timeout for a test case. Seems like a simple feature.

import puppeteer from 'puppeteer'

test('App loads', async() => {
  const browser = await puppeteer.launch({ headless: false, slowMo: 250 });
  const page = await browser.newPage();
  await page.goto('http://localhost:3000');
  await browser.close();
});
like image 853
Atav32 Avatar asked Jan 02 '23 11:01

Atav32


2 Answers

Jest's test(name, fn, timeout) function can take a 3rd parameter that specifies a custom timeout.

test('example', async () => {
  ...
}, 1000); // timeout of 1s (default is 5s)

Source: https://github.com/facebook/jest/issues/5055#issuecomment-350827560

like image 114
Atav32 Avatar answered Jan 13 '23 15:01

Atav32


You can also set the timeout globally for a suite using jest.setTimeout(10000); in the beforeAll() function:

beforeAll(async () => {

    jest.setTimeout(10000); // change timeout to 10 seconds

});
like image 33
mckenna Avatar answered Jan 13 '23 14:01

mckenna