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

查看:192
本文介绍了如何在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();
}

推荐答案

似乎值得指出,在一个有趣的问题中也对此进行了讨论:

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天全站免登陆