Search code examples
asynchronousjestjsts-jest

How can I prevent Jest from running async tests concurrently?


I'm writing a series of tests against a database. All tests take the following form:

test("Name", async ()=>{
    // I do not call done(). I didn't think I had to anymore,
    // and I get type errors if I do.
});

Unfortunately, this is causing concurrency issues. I'm not sure if that's down to background tasks on the DB, or Jest running tests concurrently. The tests work fine when run individually, so I know concurrency of some sort is the problem.

How can I make absolutely sure that Jest runs these async tests one at a time?


Solution

  • In Jest, the tests in a single file run serially in the order of appearance. However, the tests in multiple files run concurrently. This is a problem when you are running all the tests against a single database.

    To disable running the tests in multiple files concurrently, use the CLI option --runInBand in your jest command.

    For example:

    jest --runInBand
    

    If you use scripts like npm run test to run your tests, then append the --runInBand to the associated command in package.json file.