如何在 Jest 中运行并发测试,每个请求有多个测试? [英] How to run concurrent tests in Jest with multiple tests per request?

查看:30
本文介绍了如何在 Jest 中运行并发测试,每个请求有多个测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时运行我的 Jest 测试,但我遇到了一个场景的问题:

I'd like to run my Jest tests concurrently, but I'm having issues with one scenario:

我正在一个端点上测试结果,我想测试关于它的多项内容.所以在我的 beforeAll 函数中,我发出请求并存储响应,然后在多个测试中测试响应.这同步工作正常,但是当我使测试并发时,它不再允许您将变量传递到测试中,所以这是不行的.或者,我可以将请求放在测试本身中,然后期待关于响应的许多事情,但是如果出现问题,我没有细粒度查看哪里出了问题.

I'm testing the results on an endpoint, and I want to test multiple things about it. So in my beforeAll function, I make the request and store the response, and then I test the response in multiple tests. This works fine synchronously, but when I make the tests concurrent, it no longer lets you pass a variable into the test, so it's a no go. Alternatively, I can put the request in the test itself and then expect many things about the response, but then I don't have the granularity to see what went wrong if something fails.

有没有针对这种情况的解决方案?

Is there any solution for this scenario?

这有效:

let data;
beforeAll(async () => {
    data = await getDataFromRequest();
}
it('value1 should be truthy', () => {
    expect(data.value1).toBeTruthy();
}
it('value2 should be truthy', () => {
    expect(data.value2).toBeTruthy();
}

这也有效:

it.concurrent('data should have correct values', async () => {
    const data = await getDataFromRequest();
    expect(data.value1).toBeTruthy();
    expect(data.value2).toBeTruthy();
}

但我想要的是:

let data;
beforeAll(async () => {
    data = await getDataFromRequest();
}
it.concurrent('value1 should be truthy', () => {
    expect(data.value1).toBeTruthy();
}
it.concurrent('value2 should be truthy', () => {
    expect(data.value2).toBeTruthy();
}

推荐答案

似乎值得指出,在一个笑话 issue 中也有关于这个的讨论:https://github.com/facebook/jest/issues/4281

Seems worth pointing out, that there is also a discussion about this in a jest issue: https://github.com/facebook/jest/issues/4281

要点:它不是那样工作的,也不是计划好的.可能的解决方法:

Gist of it: It doesn't work that way and isn't planned. Possible workaround:

const dataPromise = getSomeDataPromise();

test.concurrent('one', async () => {
  const data = await dataPromise;
});

test.concurrent('two', async () => {
  const data = await dataPromise;
});

这篇关于如何在 Jest 中运行并发测试,每个请求有多个测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆