(Updated: )
/ #jest #javascript #node 

How to run Jest tests sequentially

Curious about Advanced Jest Testing Features?

Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.

Get "The Jest Handbook" (100 pages)

I want this

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

Author

Hugo Di Francesco

Co-author of "Professional JavaScript", "Front-End Development Projects with Vue.js" with Packt, "The Jest Handbook" (self-published). Hugo runs the Code with Hugo website helping over 100,000 developers every month and holds an MEng in Mathematical Computation from University College London (UCL). He has used JavaScript extensively to create scalable and performant platforms at companies such as Canon, Elsevier and (currently) Eurostar.

Curious about Advanced Jest Testing Features?

Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library. Get "The Jest Handbook" (100 pages)