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?
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!
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.
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/**/*.
The following worked for me:
var myObject = {
//...
};
/* istanbul ignore next */
myObject._resizeEvent = function () {
//...
};
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() {
},
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With