【问题标题】:Jest limit concurrency of integration tests开玩笑限制集成测试的并发性
【发布时间】:2020-09-04 15:13:33
【问题描述】:

如何限制测试执行的并发性?假设我只想同时并行运行 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 运行 jest,但我想这与--runInBand 相同,虽然有效,但确实会减慢整体执行时间。

【问题讨论】:

    标签: jestjs


    【解决方案1】:

    除了--maxWorkers=1,您可以提供更大的值,例如--maxWorkers=2--maxWorkers=50%

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

    jest --maxConcurrency=N
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-06
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-07
      相关资源
      最近更新 更多