node.js - 当测试用例花费更多时间时, Istanbul 尔与摩卡不会生成覆盖率报告

标签 node.js unit-testing istanbul

我正在使用 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.

关于node.js - 当测试用例花费更多时间时, Istanbul 尔与摩卡不会生成覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53849817/

相关文章:

unit-testing - Selenium 批判

javascript - Istanbul 尔代码覆盖率检查我的规范文件而不是源代码的覆盖率?

node.js - 为什么netbeans 8要安装未使用的cordova插件

java - 在 NodeJS 和 Java 中计算 HMAC sha-512

PHPUnit 错误 "Failed to open stream: No such file or directory"

c# - 在不调用 Add 方法的情况下测试 Remove 方法

javascript - Node.js Istanbul/Mocha 单元测试不打印详细信息

angular - 使用 istanbul-combine 合并覆盖率报告

node.js - 为什么在这种情况下使用try/catch一个坏主意?

node.js - 具有 React、NPM 和调试功能的 Outlook Web-App