How to run Jest tests sequentially
By default Jest runs tests in parallel with a “a worker pool of child processes that run tests” (Jest CLI docs).
As per the Jest documentation, running tests in sequence (serially), can be helpful for debugging purposes and for test run performance reasons.
Table of Contents
CLI Option to run Jest sequentially/non-parallel
The --runInBand
or -i
option of the Jest CLI allows you to run tests sequentially (in non-parallel mode).
The final command will look something like:
jest [other-options-or-params] --runInBand
or
jest [other-options-or-params] -i
Why run tests sequentially?
Jest is known to run slow on some CI providers
See Tests are Extremely Slow on Docker and/or Continuous Integration (CI) server - Jest Docs
Continuous Integration servers can have smaller CPUs which can be single-core or less cores than your local development machine while also having slower file access.
Running suite in multi-process mode is more expensive than running everything in series since the runner will keep having to context switch.
Your test suites needs to run in a pre-determined order
This is not a great reason to run the whole set of tests sequentially.
Instead of leveraging -i
/--runInBand
, try to make your suites less dependent on each other.
For some integration testing scenarios, this might not be feasible.
In that case, manually running each test suite using a shell script jest suite1.js && jest suite2.js
in a particular order might be a better solution than using -i
/--runInBand
.
Get The Jest Handbook (100 pages)
Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.
orJoin 1000s of developers learning about Enterprise-grade Node.js & JavaScript