From Testing JavaScript Applications by Lucas da Costa

This article explains what parallelism is and how Jest can help you run your tests faster.

Take 40% off Testing JavaScript Applications by entering fccdacosta into the discount code box at checkout at

Parallelism and concurrency

Let’s say that you have 4 test files which take one second each, sequentially running them would take, in total, four seconds. As the number of test files increase, so does the total execution time.

Figure 1. What happens when running tests sequentially.

To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files.

Paralellising tests

IMPORTANT:  Paralellising tests mean using different threads to run test-cases simultaneously.

Figure 2. What happens when running tests in parallel.

Paralellising tests can be beneficial if they are well isolated, but it can be problematic if they share data. If you have, for example, two test files using the same database table, you may have different results depending on the order they run.

If you can’t isolate your tests, make them run sequentially by passing Jest the runInBand option. It’s better to make your tests slow and reliable than fast and flaky.

 # To run tests sequentially
 jest --runInBand
 # Or, if you have encapsulated the `jest` command into an NPM script
 npm test -- --runInBand

In case you have tests which can be parallelised within a test suite, you can use test.concurrent to indicate which ones Jest should execute in parallel.


 describe("addItemToCart", () => {
     test.concurrent("add an available item to cart", async () => { /* */ });
     test.concurrent("add unavailable item to cart", async () => { /* */ })     test.concurrent("add multiple items to cart", async () => { /* */ })

To control how many tests run at a time, you can use the --maxConcurrencyOption and specify how many tests Jest can run simultaneously. To manage the number of worker threads spawned to run tests, you can use the --maxWorkers option and specify how many threads to spawn.

Parallelising tests can dramatically speed-up execution time. And, since tests that run fast incentivize you to run them more often, I’d highly recommend you to adopt this approach. Its only downside is that you must be careful to make sure that tests are well isolated.

That’s all for this article. This is just a small teaser of the book’s contents. If you want to see more, you can preview more on our browser-based liveBook reader here.