Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore object method for istanbul coverage report

I have a set of unit tests for my web application that run using Mocha and jsdom. I am using jsdom instead of a real browser in order to have my mocha unit tests run as fast as possible and we will have integration tests that will run selenium that will test more of real browser stuff. I am also using istanbul to generate code coverage reports however some code can not be tested with Mocha/jsdom. Take this code for example:

var myObject = {
    //...

    /* istanbul ignore next */
    _resizeEvent: function() {
        if(this.props.isActive) {
            //.9 match the scss max-height: 90%, this value needs to be kept in sync with the sass code
            this.getDOMNode().querySelector('.modal__content').style.maxHeight = Math.floor(window.innerHeight * .9) + 'px';
            this.getDOMNode().querySelector('.modal__content').style.maxWidth = Math.floor(window.innerWidth * .9) + 'px';
            this._centerPosition();
        }
    },

    //...
}

Since there is no real browser, this method can not really be tested with jsdom. The issue I am running into is that even though I have the /* istanbul ignore next */ before the object method, it is still telling me that the code inside the method is not covered (instead of just telling me it is ignored).

Is there an easy way to completely ignore object methods and all of its contents without having to add /* istanbul ignore next */ before each statement within the method?

like image 451
ryanzec Avatar asked Feb 11 '15 13:02

ryanzec


People also ask

How do I ignore a file in Istanbul?

For example, if you have a source file that is wrapped in a function expression, adding /* istanbul ignore next */ at the top of the file will ignore the whole file!

When to use istanbul ignore next?

istanbul is a JavaScript code coverage tool. Some things are hard to test, so /* istanbul ignore next */ tells istanbul to skip and not include the next thing in your source code when calculating code coverage for your project. It's a way to exclude code in your project in code coverage reports.

How do I ignore test coverage in Jest?

To ignore a file pattern for Jest code coverage, we can add the option in the Jest config. to calculate the code coverage from the paths that match patterns listed in collectCoverageFrom that don't have the exclamation mark before it. As a result, we see code coverage calculated from the src/**/*.


2 Answers

The following worked for me:

var myObject = {
//...
};

/* istanbul ignore next */
myObject._resizeEvent = function () {
//...
};
like image 184
Gil Elad Avatar answered Oct 05 '22 06:10

Gil Elad


Was facing this too, fortunately there's no need for myObject._resizeEvent. You just need to make sure that the ignore next is next to the function name.

The following works:

var myObject = {
    _resizeEvent /* istanbul ignore next */: function() {

    },
}
like image 32
Kyle Johnson Avatar answered Oct 05 '22 06:10

Kyle Johnson