【发布时间】:2023-04-06 01:05:02
【问题描述】:
我想在beforeAll() 钩子上分配一组具有值的变量,然后在test.each([]) 中使用它们,但我得到的只是未定义而不是实际值。
但是当我尝试在普通test() 中访问相同的变量时,事实证明,我的变量具有实际值并且不是未定义的。
有没有办法给变量赋值,然后在test.each([])中使用?
let movieId: string;
let serialId: string;
describe('Video page tests', () => {
beforeAll(async () => {
movieId = await videos.CreateMovie();
serialId = await videos.CreateSerial();
});
test.each([
['Deletes movie', movieId],
['Deletes serial', serialId],
])('%s', async(caseTitle, entityId) => {
reporter.description(`${caseTitle}`);
await videos.DeleteContent(entityId); //entityId is undefined
})
test('Deletes movie', async() => {
reporter.description(`Deletes movie`);
await videos.DeleteContent(movieId); //movieId is correct
})
【问题讨论】:
-
你不能。
beforeAll仍然在测试 execution 时间运行,而不是测试 discovery 时间(在评估test.each时)。这就是为什么 ID 在测试中是正确的,它在beforeAll. 之后运行
标签: typescript jestjs automated-tests each playwright