Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore required files in node.js from istanbul coverage

In my code I have var es = require('event-stream');

and in my package.json, I have

"scripts": {
    "test": "istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec",
}

I only want to cover my main file, however it covers also event-stream's files so I get things like

=============================== Coverage summary ===============================

Statements   : 24.74% ( 757/3060 )
Branches     : 5.42% ( 88/1625 )
Functions    : 15.56% ( 70/450 )
Lines        : 25.37% ( 735/2897 )
================================================================================

Is there a way to only cover my own code?

like image 379
yujinred Avatar asked Nov 06 '14 19:11

yujinred


2 Answers

Istanbul allows you to tell it to ignore code with specific comments, in the form

/* istanbul ignore <word>[non-word] [optional-docs] */

So, you can ignore a require() statement like this:

/* istanbul ignore next */
var es = require('event-stream');

The cruft generated by the require statement will be grayed out in your coverage report, and not included in the totals. See the documentation for more info: https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md

like image 58
will_wow Avatar answered Oct 24 '22 02:10

will_wow


never mind, I figured it out.

What khansolo said I have considered but when I tried it, it didn't work.

The problem is when I use sandboxed-module. Which causes any function not mocked out to be a part of the coverage.

var es = require('event-stream');
var main = SandboxedModule.require('../main', {
    requires: { 
        'gulp-s3': gulpS3,
        'knox': faux,
        'event-stream': es
    }
});

By doing this kind of Pseudo-mocking for the test. We can keep keep coverage exclusive to our own file and not 'event-stream'.

like image 28
yujinred Avatar answered Oct 24 '22 01:10

yujinred