/ #jest #javascript 

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.

unsplash-logoJason Blackeye

Looking for a new job? Take Triplebyte’s quiz and have top tech companies pitch you!

Author

Hugo Di Francesco

A Software Engineer and recovering Frontend Engineer who is big on Node.js, queues and Vue(s). He shares practical JavaScript tips for the developer who wants to get things done on Code with Hugo. University College London (UCL), MEng Mathematical Computation Graduate.

Subscribe for Enterprise Node.js and JavaScript Guides

Build your web platform with modern Node.js/JavaScript best-practices, tools and patterns