集成测试的玩笑限制并发性

时间:2019-05-07 19:47:51

标签: jestjs

如何在开玩笑的情况下限制测试执行的并发性?假设我只想同时并行运行4个测试用例。

我当前的问题是我使用真实的数据库连接进行了很多集成测试。 Jest并行执行过多的测试用例,因此经常会出现连接超时或测试数据库实例的性能显着下降的情况,因为同时有太多查询。

我所有的集成测试服都具有以下结构:

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 () => {...})
})

我已经尝试过用--maxWorkers=1来开玩笑,但是我猜这和--runInBand一样,虽然可以,但是确实减慢了整体执行时间。

1 个答案:

答案 0 :(得分:0)

除了提供--maxWorkers=1--maxWorkers=2之类的较大值外,还可以提供--maxWorkers=50%

但是,如果您试图限制并行运行的测试数量,则似乎要使用:

jest --maxConcurrency=N

笑话的documentation说:

防止Jest同时执行超过指定数量的测试。仅影响使用test.concurrent的测试。

因此,请注意,您必须通过添加.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 () => {...})
})