Jest不等待fileStream"完成"打回来

时间:2018-03-08 12:07:51

标签: javascript node.js callback jestjs

我正在使用Jest来读取一堆CSV文件并检查验证。但是,即使使用done参数,它也会给我:

    Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

      at node_modules/jest-jasmine2/build/queue_runner.js:68:21
      at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:633:19)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        13.682s

示例代码:

describe('Validate there are no duplicates', () => {
    it('Read and validate CSV files', done => {
        parseCSVs((err, result) => {
            if(err) {
                return done(err)
            }
            console.log(result)
            expect(result.idSet.size).toBe(result.validationCounter)
            done()
        })
    })
});  

被称为的函数:

parseCSVs = (callback) => {
    let validationCounter = 0
    const idSet = new Set([])
        for (const someNumber of setOfNumbers.keys()) {
            const fullDest = baseDir + "/Client" + someNumber
            Filehound.create()
            .ext('csv')
            .paths(fullDest)
            .find((err, csvFiles) => {
                if (err) {
                    callback(err, null)
                    return
                } else {
                    for (file of csvFiles) {
                        const csvFileStream = fs.createReadStream(file);
                        csv()
                        .fromStream(csvFileStream)
                        .on("json", (jsonObj) => {
                            const validationID = Object.values(jsonObj).toString().split(';')[2]
                            idSet.add(validationID)
                            validationCounter++
                        })
                        .on("finish", () => {
                            callback(null, {validationCounter, idSet}) // Why doesn't it let this async operation finish?
                        })
                    }
                }
            });
        }
}

在fileStream上,我等待"完成"为每个文件,然后返回回调。但是,即使添加了done测试,由于没有回调,它会在五秒后退出。这是为什么?我是否可以在等待时间超过5秒的Jest中进行异步操作?

0 个答案:

没有答案