1.1 Run tests sequentially
By default Jest runs tests in parallel with a “a worker pool of child processes that run tests” (Jest CLI docs). This is great when you’ve got a machine with enough resources to take advantage of the multiplexing.
As per the Jest documentation, running tests in sequence (serially), can be helpful for debugging purposes and for test run performance reasons.
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 is an alternative to -i
/--runInBand
.