How to run Jest tests sequentially?
CLI options are documented and also accessible by running the command jest --help
.
You'll see the option you are looking for : --runInBand
.
Use the serial test runner:
npm install jest-serial-runner --save-dev
Set up jest to use it, e.g. in jest.config.js:
module.exports = {
...,
runner: 'jest-serial-runner'
};
You could use the project feature to apply it only to a subset of tests. See https://jestjs.io/docs/en/configuration#projects-arraystring--projectconfig
It worked for me ensuring sequential running of nicely separated to modules tests:
1) Keep tests in separated files, but without spec/test
in naming.
|__testsToRunSequentially.test.js
|__tests
|__testSuite1.js
|__testSuite2.js
|__index.js
2) File with test suite also should look like this (testSuite1.js):
export const testSuite1 = () => describe(/*your suite inside*/)
3) Import them to testToRunSequentially.test.js
and run with --runInBand
:
import { testSuite1, testSuite2 } from './tests'
describe('sequentially run tests', () => {
testSuite1()
testSuite2()
})
I'm still getting familiar with Jest, but it appears that describe blocks run synchronously whereas test blocks run asynchronously. I'm running multiple describe blocks within an outer describe that looks something like this:
describe
describe
test1
test2
describe
test3
In this case, test3
does not run until test2
is complete because test3
is in a describe block that follows the describe block that contains test2
.