【发布时间】:2018-08-15 13:22:28
【问题描述】:
我正在通过 Jest 在 Node 中编写一个测试,在该测试中我读取了一堆 CSV 文件,解析特定列,将其放入一个集合中,然后针对该集合验证一个计数器以确定是否存在重复。
但是,解析 CSV 值的操作设置起来相当冗长(3-4 分钟)。然而,Jest 不会等待:它只是立即通过测试。
示例代码:
describe('Test that all values are unique', () => {
let validationCounter = 0
const validationSet = new Set([])
it('Read all csv files', () => {
for (const number of dirsToCheck.keys()) {
const baseDest = baseDir + "/something" + number
Filehound.create()
.ext('csv')
.paths(baseDest)
.find((err, csvFiles) => {
if (err) {
return console.error("handle err", err)
} else {
for (file of csvFiles) {
csv({
flatKeys: true,
})
.fromFile(file)
.on("json", (jsonObj) => {
const designatedValue = Object.values(jsonObj).toString().split(';')[2]
validationSet.add(designatedValue)
validationCounter++
console.log(designatedValue) // Will make everything "hang" until this is finished, if logged
})
}
}
});
}
})
it('Validate array counter to the unique set', () =>{
console.log(validationCounter) // Returns 0?
console.log(validationSet.size) // Also 0?
expect(uniqueVurderingsejendomsIds.size).toBe(validationCounter); // Somehow passes?
})
});
如果我console.log(designatedValue),它将持续运行整整四分钟,然后在完成将它们添加到集合后退出 - 不运行测试。
如果我删除这个 console.log 语句,Jest 将几乎立即(2 秒)通过整个测试套件
发生了什么,为什么 Jest 不等待这个操作?它立即通过了所有测试,然后 Jest 挂起而不退出。这是非常奇怪的行为。
【问题讨论】: