【发布时间】:2020-10-21 02:58:14
【问题描述】:
我想在 Jest 中的 for 循环内动态运行多个测试,但我不确定在哪里放置我的测试环境所需的异步代码。
一个可行的选择是将所有异步代码放在一个测试函数中,并为 for 循环的每次迭代执行一个断言语句。
describe('Database Testing', () => {
test(`Testing ${items}`, async () => {
const items = await getItems(); //<-- asynchronous code
for (const item of items) {
expect('hi').toEqual('hi');
}
});
});
但是,如果测试失败,我将无法确定 assert 语句在哪个循环中失败。相反,我想要一个类似于下面的结构,在该结构中,我为 for 循环的每次迭代动态运行测试。
describe('Database Testing', () => {
const items = await getItems(); //<-- asynchronous code
for (const item of items) {
test(`Testing ${item}`, async () => {
expect('hi').toEqual('hi');
});
};
});
由于 describe 函数的同步性质,我无法运行异步代码。但是,如果我在 describe 函数中使用 async 关键字,则会收到错误消息“不支持从“describe”返回 Promise”。
我应该在哪里运行异步代码?
【问题讨论】:
标签: javascript asynchronous testing jestjs