ChatGPT解决这个技术问题 Extra ChatGPT

Are tests inside one file run in parallel in Jest?

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?


m
mikemaccana

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.


Well, the problem is, that beforeEach and afterEach would be useless in a variety of cases when tests are async functions, because if Jest were to run async tests in parallel, then it's no longer possible to reset state or clean up after each test, which would make testing very prone to bugs. beforeEach and afterEach serve to set up or clean up between tests, and in order for this to be meaningful, tests need to be executed in series, not in parallel. It would be a bad API design for Jest (or any runner) to run async function tests in parallel...
By the way @trusktr Jest DOES run tests in parallel, just not ones in the same file. So you can run into issues with interference between tests if they are running on the same database.
@JaimeSangcap We currently use a different database for each test suite. In the jest config we have setupFiles: ['<rootDir>/src/testing/integration/targetRandomDatabase.js'], and in targetRandomDatabase.js we set a random fresh database URL each time. The setup files are run separately for each test suite, so this prevents interference. We use mongodb-memory-server to run a fresh mongodb for each run of jest, so the databases are thrown away automatically.
@JaimeSangcap Sorry, I moved to Karma.
The --runInBand CLI flag prevents jest from running files in parallel
B
Ben Winding

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.


" Multiple describe() statements will execute in parallel though, even if they're in the same file. ", I tried two describe() in one test file, and first describe has a async test case, but found they execute in series.
I wrongly assumed that describe statements were the highest level, I guess each file's tests are executed in series regardless of describe wrappers
K
Klesun

Note, you may get side effects of two tests running in parallel if one of them times out after 5 seconds - jest stops waiting for the timed out test, but it's code continues executing, in parallel with the following test jest picks up.

(pulled out quite an amount of hair before I realised that this was the reason of the side effects that led me here)


Where is this documented?
Additionally, afterAll is called when the test timeouts, but the async code will finish executing, so if you are doing things in afterAll, such as disconnecting your database connection, you might get error messages intermixed with subsequent test. You can fix this by calling jest with --bail 1
L
LeOn - Han Li

You can use test.concurrent('test run concurrently', () => { ... }) if you want to run them in parallel inside one file is too slow. It is a bit buggy and not well documented, but at least there's a way there.

One thing I notice is it does not wait for async stuff in the beforeAll() so you need some of your own trick(like setInterval to wait) to make it work as expected.


A
Ali80

To make test files run sequentially use -i or --runInBand flag

jest --runInBand

ref: jest docs

Weirdly enough, running the tests sequentially finishes faster on my M1 Mac!, better test for you use case


This does not answer the question. OP is asking if tests run sequentially if they are in one file rather than being in different files in which case it should run in parallel by default as stated in JEST docs.
@Sphinx the question seems to be looking for a way to make sure the tests are run sequentially at least in one file, I was also looking for the same thing!