Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest limit concurrency of integration tests

Tags:

jestjs

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.

like image 955
Vetterjack Avatar asked May 07 '19 19:05

Vetterjack


People also ask

Do Jest tests run concurrently?

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.

How do you only run one test in Jest?

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.

Is Jest single threaded?

By default, Jest will run on all available CPU threads, using one thread for the cli process and the rest for test workers.

Does Jest cache test results?

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.


1 Answers

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 () => {...})
})
like image 196
Peter W Avatar answered Oct 27 '22 10:10

Peter W