测试执行后如何退出Mocha?

问题描述

我希望在执行所有测试后退出Mocha,以便Istanbul可以检查代码覆盖率,我在--exit中使用了package.json,但似乎无法正常工作。

我想念什么吗?

"scripts": {
"test": "istanbul cover node_modules/mocha/bin/_mocha test/Testcases/ --exit" 
 }

所有测试文件都位于test/Testcases/文件夹下,并且在全部执行后,mocha应该退出

P.S。我使用的是Windows操作系统,因此我要手动引用bin文件

解决方法

使用--exit应该可以。

顺便说一下,我使用它,我认为无论有无覆盖,执行起来都更加灵活:

"scripts": {
    "test": "mocha tests --timeout 4000 --reporter mochawesome --exit","coverage": "nyc --reporter=html --reporter=text npm run test"
  }

因此,如果您运行npm run test,则只会得到测试,但是使用npm run coverage,覆盖率也会显示出来。

请注意,coverage遵循以下模式:

nyc + arguments + mocha execution

但是仅使用一行,这也可以:

"test": "nyc --reporter=html --reporter=text mocha tests --timeout 4000 --reporter mochawesome --exit"

删除其他参数:

"test": "nyc --reporter=html mocha tests --exit"