最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to ignore required files in node.js from istanbul coverage - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Nov 6, 2014 at 19:59 yujinredyujinred 1052 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Istanbul allows you to tell it to ignore code with specific ments, 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./gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md

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

发布评论

评论列表(0)

  1. 暂无评论