Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are tests inside one file run in parallel in Jest?

Tags:

jestjs

Jest states in docs: "Jest virtualizes JavaScript environments and runs tests in parallel across worker processes."

But what about multiple tests inside one file, do they run in parallel or this statement applies just to the test files? Can I assume that tests in one file run in order of appearance and in serial?

like image 582
Jarda Avatar asked Oct 03 '16 12:10

Jarda


People also ask

Does Jest run all tests in parallel?

To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files.

Are Jest tests run sequentially?

Jest will execute different test files potentially in parallel, potentially in a different order from run to run. 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.

Do Jest tests run asynchronously?

It's common in JavaScript for code to run asynchronously. When you have code that runs asynchronously, Jest needs to know when the code it is testing has completed, before it can move on to another test.

How do I run a Jest test on one file?

If you are running npm >= 5.2. 0 and you have installed Jest locally as a devDependencies with npm i -d jest , you can run Jest on a particular file by doing npx jest /path/to/your/spec.


2 Answers

Yes, you can safely assume tests inside a single file will run in the order of appearance. You could prove this by putting a console.log in each it block.

It's probably worth mentioning that it's generally bad practice to rely on the order of execution / external state...and you never know, Jest (or the current underlying test runner, Jasmine) may decide to run them in a random order in a newer version.

like image 158
riscarrott Avatar answered Oct 02 '22 17:10

riscarrott


Jest in 2020

To add a bit more information on this, async tests are run in series inside of a describe() statement. This is useful for IO/Database setup and cleanup functions. Just take a look at the following example:

some.spec.js

describe("my asynchronous tests", () => {   beforeEach(async () => {     console.log('> setup test')     // SOMETHING ASYNCHRONOUS   });   afterEach(async () => {     console.log('< teardown test')     // SOMETHING ASYNCHRONOUS   });    test("test 1", async () => {     console.log('-- starting test 1');     // SOMETHING ASYNCHRONOUS     console.log('-- finished test 1');   }, 100000);    test("test 2", async () => {     console.log('-- starting test 2');     // SOMETHING ASYNCHRONOUS     console.log('-- finished test 2');   }, 100000); }); 

Outputs:

> setup test -- starting test 1 -- finished test 1 < teardown test > setup test -- starting test 2 -- finished test 2 < teardown test 

Multiple describe() statements will execute in parallel though, even if they're in the same file.

like image 40
Ben Winding Avatar answered Oct 02 '22 16:10

Ben Winding