node.js Mocha 代码覆盖率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16633246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:32:32  来源:igfitidea点击:

Code coverage with Mocha

node.jsunit-testingcode-coveragemocha

提问by tusharmath

I am using Mocha for testing my NodeJS application. I am not able to figure out how to use its code coverage feature. I tried googling it but did not find any proper tutorial. Please help.

我正在使用 Mocha 来测试我的 NodeJS 应用程序。我不知道如何使用它的代码覆盖功能。我尝试使用谷歌搜索它,但没有找到任何合适的教程。请帮忙。

回答by Dan Kohn

You need an additional library for code coverage, and you are going to be blown away by how powerful and easy istanbulis. Try the following, after you get your mocha tests to pass:

你需要一个额外的代码覆盖库,你会被伊斯坦布尔的强大和简单所震撼。在您通过 mocha 测试后,请尝试以下操作:

npm install nyc

Now, simply place the command nyc in front of your existing test command, for example:

现在,只需将命令 nyc 放在现有测试命令的前面,例如:

{
  "scripts": {
    "test": "nyc mocha"
  }
}

回答by lifeisfoo

Now (2020) the preferred way to use istanbulis via its "state of the art command line interface"nyc.

现在(2020 年)使用istanbul的首选方式是通过其“最先进的命令行界面” nyc

Setup

设置

First, install it in your project with

首先,将它安装在您的项目中

npm i nyc --save-dev

Then, if you have a npm based project, just change the test script inside the scriptsobject of your package.jsonfile to execute code coverage of your mochatests:

然后,如果您有一个基于 npm 的项目,只需更改package.json文件scripts对象内的测试脚本即可执行mocha测试的代码覆盖率:

{
  "scripts": {
    "test": "nyc --reporter=text mocha"
  }
}

Run

Now run your tests

现在运行你的测试

npm test

and you will see a table like this in your console, just after your tests output:

您将在控制台中看到这样的表格,就在您的测试输出之后:

Istanbul Nyc Mocha code coverage

伊斯坦布尔纽约摩卡代码覆盖率

Customization

定制

Html report

html报告

Just use

只需使用

nyc --reporter=html

instead of text. Now it will produce a report inside ./coverage/index.html.

而不是text. 现在它会在里面产生一个报告./coverage/index.html

Report formats

报告格式

Istanbul supports a wide range of report formats. Just look at its reports libraryto find the most useful for you. Just add a --reporter=REPORTER_NAMEoption for each format you want. For example, with

伊斯坦布尔支持多种报告格式。只需查看其报告库即可找到对您最有用的报告。只需--reporter=REPORTER_NAME为您想要的每种格式添加一个选项。例如,与

nyc --reporter=html --reporter=text

you will have both the console and the html report.

您将同时拥有控制台和 html 报告。

Don't run coverage with npm test

不要使用 npm test 运行覆盖率

Just add another script in your package.jsonand leave the testscript with only your test runner (e.g. mocha):

只需在您的脚本中添加另一个脚本,并将脚本package.json保留在test您的测试运行器中(例如 mocha):

{
  "scripts": {
    "test": "mocha",
    "test-with-coverage": "nyc --reporter=text mocha"
  }
}

Now run this custom script

现在运行这个自定义脚本

npm run test-with-coverage

to run tests with code coverage.

以代码覆盖率运行测试。

Force test failing if code coverage is low

如果代码覆盖率低,则强制测试失败

Fail if the total code coverage is below 90%:

如果总代码覆盖率低于 90%,则失败:

nyc --check-coverage --lines 90 

Fail if the code coverage of at least one file is below 90%:

如果至少一个文件的代码覆盖率低于 90%,则失败:

nyc --check-coverage --lines 90 --per-file

回答by jsan

Blanket.js works perfect too.

Blanket.js 也很完美。

npm install --save-dev blanket

npm install --save-dev blanket

in front of your test/tests.js

在你的 test/tests.js 前面

require('blanket')({
    pattern: function (filename) {
        return !/node_modules/.test(filename);
    }
});

run mocha -R html-cov > coverage.html

mocha -R html-cov > coverage.html