当测试用例花费更多时间时,带有 mocha 的伊斯坦布尔不生成覆盖率报告

istanbul with mocha not generating coverage report when a testcase takes more time

我正在使用 mocha 测试我的 js 文件。一些测试用例需要时间来解决响应,所以我使用了超时。完整的命令看起来像

istanbul cover _mocha test/sol-verifier.js -- --timeout 300000

问题是,在进行这样的覆盖时,我没有创建覆盖报告,它只成功运行测试用例并停止(不终止)。我该如何解决?

我的代码在 node.js 中,它没有创建任何服务器。

此外,当我删除 --timeout 标志并注释掉需要更多时间的测试用例时。它工作正常并生成覆盖率报告。

根据我的发现,问题不在 --timeout 标志中,当测试用例的解决时间比平时长时,不会生成报告。

您可以向测试套件函数添加一个 done 参数,并在每个测试函数的末尾调用 done()

it('test expectation', function(done) {
    // test asynchronous code
    // call done() to terminate test and proceed to the next test
    done();
}

或者,在 mocha 上使用 --exit 标志尝试 运行 测试:

istanbul cover _mocha --exit test/sol-verifier.js -- --timeout 300000

根据 docs:

To avoid false positives and encourage better testing practices, Mocha will no longer automatically kill itself via process.exit() when it thinks it should be done running.

If the mocha process is still alive after your tests seem "done", then your tests have scheduled something to happen (asynchronously) and haven't cleaned up after themselves properly. Did you leave a socket open?

Supply the --exit flag to use pre-v4 behavior.