Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jest guarantee it will run tests in a single file sequentially?

My understanding of jest from observation is that it provides concurrent execution of tests by spawning helper processes and distributes test files to the workers to execute as they finish their current test files.

That suggests to me that jest won't attempt to execute tests in an individual test file concurrently. So I would expect that the following test would always pass (without needing to pass --runInBand):

describe('counting test', () => {
  let variable = 0;

  it('should start as 1', () => {
    variable += 1;
    expect(variable).toEqual(1);
  });
  it('should change to 2', () => {
    variable += 1;
    expect(variable).toEqual(2);
  });
});

I.e. the second test is always run after the first test has finished. Is that safe, and is there an official document somewhere that specifies this behaviour? I couldn't find one.

like image 822
daphtdazz Avatar asked Jan 02 '18 15:01

daphtdazz


1 Answers

Since this didn't have an official answer, I added one to the jest documentation after some further research / experimentation (and it was signed off by one of their moderators).

So, yes, jest runs each test in a file sequentially, waiting for each to finish before moving onto the next. This is now described in Setup and Teardown.

Further note that describe blocks are all executed before any of the test blocks.

For reference, the code that implements this is mostly in jest-circus/src/run.ts and eventHandler.ts.

like image 51
daphtdazz Avatar answered Nov 15 '22 20:11

daphtdazz