Are tests inside one file run in parallel in Jest?

后端 未结 3 1456
谎友^
谎友^ 2021-02-01 13:47

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 t

3条回答
  •  太阳男子
    2021-02-01 14:21

    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.

提交回复
热议问题