我正在尝试将文件加载到数组中,然后对它们运行测试。这是我的代码:
let files: string[] = []
describe.only("Name of the group", () => {
beforeAll(() => {
files = ["a", "b"]
})
test.each(files)("runs", f => {
console.log(f)
})
})
但是,我得到
Error:
.each
called with an empty Array of table data.
我究竟做错了什么?
谢谢!
最佳答案
test.each
需要一个表值作为输入。这意味着一个数组数组。但这是自动修复的,所以不用担心。
但是调用顺序在这里很重要!请注意,测试是在实际运行之前定义的。所以,beforeAll
将在定义测试后运行。这意味着 files
注册测试时不会定义数组。
为了解决这个问题,您需要确保 files
数组已填充 在读取和注册测试之前
所以是这样的:
const files: string[][] = [ ['test1'],['test2'] ];
describe('Something Something', () => {
describe('Name of the group', () => {
test.each(files)('runs %s', (f) => {});
});
});
关于javascript - 无法使用 jest.each 运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60093463/