How to run a list of test suites in a single file concurrently in jest?

Viewed 21

I use jest with puppeteer for testing my frontend Js library. I have stored a list of json objects in a file. Where each json object in the list is the data for a test suite. Initially, I had them stored in separate files. One file for each suite, so they used to run concurrently and finish quickly. I moved all the test suites data into a single file during refactoring. This helps me search and edit test cases better. But after adding everything to a single file, I am now looping through the testSuites data list and calling test function for each testSuite. The problem is that jest is now running things sequentially and this is taking 5x more time than the concurrent running of tests.

Is there a way to run testSuites in a single file concurrently ?

1 Answers

Assuming you are not running test with the --runinband flag, the simple answer is yes but it depends

There is a pretty comprehensive GitHub issue jest#6957 that explains certain cases of when tests are run concurrently or in parallel. But it seems to depend on a lot of edge cases where jest tries its best to determine the fastest way to run the tests given the circumstances.

To my knowledge there is no way to force jest to run in parallel.


Aside

Have you considered using playwright instead of puppeteer with jest? Playwright has their own internally built testing library called @playwright/test that is used in place of jest with a similar API. This library allows for explicitly defining test groups in a single file to run in parallel (i.e. test.describe.parallel) or serially (i.e. test.describe.serial). Or even to run all tests in parallel via a config option.

// parallel
test.describe.parallel('group', () => {
  test('runs in parallel 1', async ({ page }) => {});
  test('runs in parallel 2', async ({ page }) => {});
});

// serial
test.describe.serial('group', () => {
  test('runs first', async ({ page }) => {});
  test('runs second', async ({ page }) => {});
});
Related