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
-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
jest [other-options-or-params] -i
Why run tests sequentially?
Jest is known to run slow on some CI providers
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
--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
Subscribe to Code with Hugo
Get all the posts of the week before anyone else in your inbox
Looking for a new job? Take Triplebyte’s quiz and have top tech companies pitch you!