Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest: Difference betwen --runInBand and --maxWorkers 1

When is it appropriate to use each of --runInBand or --maxWorkers 1 options?

If my intent is to run all tests in sequence (one at a time, in order), which of these is the right option?


Extra detail:

I'm using Jest to test a NodeJs express application, with integration tests hitting the HTTP endpoints via supertest. This may not make any difference to the answer, just mentioning in case it is relevant.

Here's the Jest CLI reference:

https://facebook.github.io/jest/docs/cli.html

Relevant parts:

--maxWorkers=<num>

Alias: -w. Specifies the maximum number of workers the worker-pool will spawn for running tests. This defaults to the number of the cores available on your machine. It may be useful to adjust this in resource limited environments like CIs but the default should be adequate for most use-cases.

--runInBand

Alias: -i. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.

like image 854
bguiz Avatar asked Mar 06 '17 22:03

bguiz


People also ask

What is -- runInBand?

--runInBand ​Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.

How do you only run one test in Jest?

So to run a single test, there are two approaches: Option 1: If your test name is unique, you can enter t while in watch mode and enter the name of the test you'd like to run. Option 2: Hit p while in watch mode to enter a regex for the filename you'd like to run.

Are Jest tests run in parallel?

Each time a test run completes, the global environment is automatically reset for the next. Since tests are standalone and their execution order doesn't matter, Jest runs tests in parallel. This means that even if you have hundreds of unit tests you can run them frequently without the fear of having to wait.


2 Answers

There is no difference. Here's the method where it gets read from the args object:

export default function getMaxWorkers(argv: Argv): number {   if (argv.runInBand) {     return 1;   } else if (argv.maxWorkers) {     return parseInt(argv.maxWorkers, 10);   } else {     const cpus = os.cpus().length;     return Math.max(argv.watch ? Math.floor(cpus / 2) : cpus - 1, 1);   } } 

original source code on github

like image 115
eremzeit Avatar answered Oct 18 '22 00:10

eremzeit


--runInBand and --maxWorkers=1 have the same behaviour.

like image 20
Ersel Aker Avatar answered Oct 17 '22 23:10

Ersel Aker