typescript - 如何在测试中使用在 beforeAll() 钩子中分配了值的变量
问题描述
我想在 hook 处分配一堆具有值的变量,beforeAll()
然后在 中使用它们test.each([])
,但我得到的只是未定义而不是实际值。
但是当我尝试在 normal 中访问相同的变量时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
})
解决方案
事实证明,我不能使用test.each
在钩子上分配的变量beforeAll
,所以我想出了一个解决我的问题的方法
let content: string[] = [];
describe('Video page tests', () => {
beforeAll(async () => {
content.push(await videos.CreateMovie());
content.push(await videos.CreateSerial());
});
test.each([
['Deletes movie', 0],
['Deletes serial', 1],
])('%s', async(caseTitle, entityId) => {
reporter.description(`${caseTitle}`);
await videos.DeleteContent(content[entityId]);
})
推荐阅读
- javascript - ng-repeat 和选定的行
- macos - 确定 NSBezierPath 边界以确保在 macOS 上正确重绘的正确方法是什么
- ibm-mobilefirst - IBM Mobilefirst 8 与 configure-tomcat-mysql.xml 配置文件有关的错误
- asp.net-core-mvc - ASP.net Core 2.1 MVC - 脚手架不使用标签助手?
- java - 如果文件没有已知的扩展名,我如何告诉 Java 使用系统的默认文本编辑器打开文件?
- jquery - Bootstrap 多选不会显示选项
- powershell - 将 Get-ADUser 的属性分配给没有标题的变量
- c# - Unity 中的 Photon 似乎无缘无故地实例化了两个玩家
- xcode - 录音机:默认扬声器播放
- javascript - 为什么我的 javascript 不能识别其中有换行符的字符串?