Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Jest --runInBand speed up tests?

Tags:

testing

jestjs

I read that the --runInBand flag speeds up Jest test duration by 50% on CI servers. I can't really find an explanation online on what that flag does except that it lets tests run in the same thread and sequentially.

Why does running the test in the same thread and sequentially make it faster? Intuitively, shouldn't that make it slower?

like image 917
Yangshun Tay Avatar asked May 09 '17 08:05

Yangshun Tay


People also ask

What is the fastest Jest?

Wallaby is the fastest available JavaScript test runner. Jest in watch mode (in the best case scenario) re-runs all tests in all test files related to changed files based on hg/git uncommitted files.

Does Jest run tests sequentially?

Per file, it will run all describe blocks first and then run tests in sequence, in the order it encountered them while executing the describe blocks. If you want to run files in sequence as well, run Jest with the --runInBand command line flag.

Why are my Jest tests slow?

Jest is slow on Windows Unfortunately Jest tests run significantly slower on Windows machines than on Mac OS and Linux due to slower crawling of the Windows file system.


2 Answers

Reading your linked page and some other related sources (like this github issue) some users have found that:

...using the --runInBand helps in an environment with limited resources.

and

... --runInBand took our tests from >1.5 hours (actually I don't know how long because Jenkins timed out at 1.5 hours) to around 4 minutes. (Note: we have really poor resources for our build server)

As we can see, those users had improvements in their performances on their machines even though they had limited resources on them. If we read what does the --runInBand flag does from the docs it says:

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.

Therefore, taking into consideration these comments and the docs, I believe the improvement in performance is due to the fact that now the process runs in a single thread. This greatly helps a limited-resource-computer because it does not have to spend memory and time dealing and handling multiple threads in a thread pool, a task that could prove to be too expensive for its limited resources.

However, I believe this is the case only if the machine you are using also has limited resources. If you used a more "powerful" machine (i.e.: several cores, decent RAM, SSD, etc.) using multiple threads probably will be better than running a single one.

like image 60
DarkCygnus Avatar answered Sep 21 '22 19:09

DarkCygnus


When you run tests in multi-threads, jest creates a cache for every thread. When you run with --runInBand jest uses one cache storage for all tests.

I found it after runs 20 identical tests files, first with key --runInBand, a first test takes 25 seconds and next identical tests take 2-3s each.

When I run tests without --runInBand key, each identical test file executes in 25 seconds.

like image 38
Ihor Bogdosarov Avatar answered Sep 20 '22 19:09

Ihor Bogdosarov