How to make Istanbul generate coverage for all of my source code?

坚强是说给别人听的谎言 提交于 2019-12-08 14:37:50

问题


Currently Istanbul is only generating coverage for files that are used in my tests, which is okay, but seems to defeat the purpose of having coverage somewhat.

I have no Istanbul configuration, and am invoking it via npm test with the following script string:

$ istanbul cover _mocha -- -R dot --check-leaks --recursive test/

Is there a way to generate coverage for all of my source code?


回答1:


Found the answer, I think I'm partly lucky that the directory structure I have chosen allows me to use this option, but my test command is now:

$ istanbul --include-all-sources cover _mocha -- -R dot --recursive test/

The --include-all-sources is the important part.




回答2:


Istanbul recommends using nyc in order to check code coverage. It suggests an approach like this:

nyc mocha

After running this command, we'll get the coverage report. But there is a couple of pitfalls.

First of all, by default mocha is looking for tests in folder test. In order to override it, we have to set our own path in file mocha.opts like this:

nyc mocha --opts ./mocha.opts

And mocha.opts contains such code, for example:

spec/unit/back-end/**/*.spec.js

Another problem is that by default nyc checks coverage of only required files, that is your question is about. The solution is to set two options for nyc (I run test as an npm script so I set options in package.json). Here is the code:

"nyc": {
  "all": true,
  "include": [
    "routes/*.js",
    "routes/**/*.js",
    "models/*.js"
  ]
},
"scripts": {
  "mocha": "nyc mocha --opts ./mocha.opts",
}

Another way to achieve it is to set not include, but exclude option in order to exclude from coverage checking inappropriate files. It's strange, but the sole option all doesn't work, it requires include or exclude options. You can get more info about nyc options via nyc --help.

P.S. I don't know nyc and mocha deeply and I'm only based on my own experience.




回答3:


For generating coverage for all files, have the following in your package.json

"istanbulCoverage": "nyc --reporter=lcov --reporter=text-lcov --all -x \"./node_modules/\" -x \"./coverage/\" check-coverage --functions 90 npm run test"

Here --all flag fetches all the files in your project. If u want to exclude specific files or folders, you can use -x option.

Apart from this, if you want to specify coverage rate for you application, then use check-coverage option to specify the threshold. In my case, I've specified functions to have a coverage threshold of 90%. Otherwise, the coverage report generation fails.(i am running my karma test after coverage report generation).

Hope this helped:)




回答4:


In my case, --include-all-sources did not work for me. Files that were not require-d were still excluded from the final coverage report.

Eventually, I came across this issue on the istanbul GitHub where the maintainer stated:

Yes, that is expected behavior. istanbul works by hooking require so if a file is never require-d it is as if it doesn't exist.

The only fool-proof solution that I found was to manually require all files that I wanted to include in my coverage report. I create the file include-all.test.js alongside my other test scripts and added the following bit of code:

var glob = require( 'glob' )
var path = require( 'path' );

glob.sync( './path/to/js/code/*.js' ).forEach( function( file ) {
    // we don't care about errors here, we just want to require the file
    try {
        require( path.resolve( file ) );
    } catch(e) {}
});

This will absolutely ensure that your untested files are included in the istanbul coverage report.



来源:https://stackoverflow.com/questions/27606071/how-to-make-istanbul-generate-coverage-for-all-of-my-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!