question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document how parallelization works in Jest

See original GitHub issue

🚀 Feature Proposal

A piece of official documentation stating exactly how parallelization works in Jest, including whether it’s safe to assume that all suites and test cases within a single file will always run serially in source order.

Motivation

It is basic information that should be officially documented.

Pitch

I’ve searched online for answers to this, and I can only find equivocal Stack Overflow threads and disagreement. From experimenting, I think it works like this:

  • Individual files are run in parallel (unless you enable the runInBand option). So it is not safe for multiple test files to share a mutable data store.
  • All describe and test blocks within a file always run in serial, in declaration order. So you can safely mutate a module-scoped variable across several tests with predictable results (even when some tests are nested more deeply than others in describe trees). This can be useful when testing a series of mutations to a piece of state.

That’s how Jest seems to work today. But I want to see docs stating if that’s the intended behaviour, so I can be sure it won’t suddenly change without warning in a minor performance update.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:55
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
andreabisellocommented, Jan 3, 2020

So the only way to runs tests in parallel way is to create one testsuite (one file) for every test? i thought jest tries always to runs test in a parallel way if --runInBand is not set, but making a try it looks like it runs in parallel testsuites, not tests.

You can understand the suites are running in a parallel way because the command prompt

say RUNS a lot of file together

image

even if you put more test in a file (a testsuite) they are runned sequentially both they are in a describe block or N describe block :

i make this experiment : every test wait 5000ms before ends

in a single describe block, it tooks more than 10 000 ms to ends

describe('describe block 1', () => {

    test("1", async () => {
        await new Promise(resolve => setTimeout(resolve, 5000));
        expect(1).toBe(1)
    })

    test("2", async () => {
        await new Promise(resolve => setTimeout(resolve, 5000));
        expect(1).toBe(1)
    })

})

image

splitting in two describe block,

describe('describe block 1', () => {

    test("1", async () => {
        await new Promise(resolve => setTimeout(resolve, 5000));
        expect(1).toBe(1)
    })


})

describe('describe block 2', () => {

    test("2", async () => {
        await new Promise(resolve => setTimeout(resolve, 5000));
        expect(1).toBe(1)
    })

})

nothing changed

image

splitting in two different files, they runs in a parallel way (there are 3 seconds of overhead)

image

but i don’t like so much to put one test in one file , because it’s very time consuming. what i’m doing wrong?

thanks.

6reactions
Luanfcommented, Feb 25, 2019

Finished a first attempt at #7984 I also added some information regarding test executing order which I thought made sense to be in the same place as the parallelization topic. Would be glad to have some feedback on this, I’m unsure if I modified the correct places. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Are tests inside one file run in parallel in Jest? - Stack Overflow
Multiple describe() statements will execute in parallel though, even if they're in the same file. Share.
Read more >
The Value of Concurrency in Tests - Manning
To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files.
Read more >
Parallelizing Jest with GitHub Actions - imhoff.blog
To parallelize our tests, we can use the matrix strategy offered by GitHub Actions. In the workflow file below, we hard-code test files...
Read more >
Jest parallelization, globals, mocks, and squawkless tests
#Tests in different files ARE run in parallel​​ Let's take another example where we use a global variable, and then two different tests...
Read more >
Getting the Jest tests to run in Parallel - Part 28 - YouTube
... the tests so they can now run in parallel.Code: https://github.com/benawad/graphql-ts-server-boilerplate/tree/28_parallel_testing----Pa.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found