How can I limit the concurrency of test execution in jest? Let's say I only want to run 4 test cases in parallel at the same time.
My current problem is that I have a lot of integration tests using a real database connection. Jest executes too much test cases in parallel, thus often connections timeout or the performance of my test database instance drops significantly because there are too many queries at the same time.
All of my integration test suits have the following structure:
describe('<functionality x>', () => {
test('case 1', async () => {...})
test('case 2', async () => {...})
test('case 3', async () => {...})
})
describe('<functionality y>', () => {
test('case 1', async () => {...})
test('case 2', async () => {...})
test('case 3', async () => {...})
})
I already tried running jest with --maxWorkers=1
, but I guess this is the same as --runInBand
, which works, but really slows down the overall execution time.
Each time a test run completes, the global environment is automatically reset for the next. Since tests are standalone and their execution order doesn't matter, Jest runs tests in parallel.
So to run a single test, there are two approaches: Option 1: If your test name is unique, you can enter t while in watch mode and enter the name of the test you'd like to run. Option 2: Hit p while in watch mode to enter a regex for the filename you'd like to run.
By default, Jest will run on all available CPU threads, using one thread for the cli process and the rest for test workers.
Even distribution of test suites across workers If you're running your test suites in parallel (enabled by default), jest will cache information about how long each of your test suites takes to run.
Rather than --maxWorkers=1
, you could provide a larger value like --maxWorkers=2
or --maxWorkers=50%
.
However, if you are trying to limit the number of tests running in parallel, it seems like you want to use:
jest --maxConcurrency=N
Jest's documentation says:
Prevents Jest from executing more than the specified amount of tests at the same time. Only affects tests that use test.concurrent.
so, note you have to modify your tests by adding the .concurrent
:
describe('<functionality x>', () => {
test.concurrent('case 1', async () => {...})
test.concurrent('case 2', async () => {...})
test.concurrent('case 3', async () => {...})
})
describe('<functionality y>', () => {
test.concurrent('case 1', async () => {...})
test.concurrent('case 2', async () => {...})
test.concurrent('case 3', async () => {...})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With