简体   繁体   中英

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?

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'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM