istanbul
istanbul copied to clipboard
how to ignore a catch statement?
this does not work:
try {
} /* istanbul ignore next */ catch (err) {
}
You have to move it into the catch to ignore the catch:
try {
} catch (e) {
/*istanbul ignore next*/
}
i want to ignore the entire block though, not the first statement in the block
Yeah, this is not good. Needs fixing in the instrumenter
I think the problem was something to do with figuring out where the comment is w.r.t the catch clause. Need to take a look.
Until we get a patch in for this, you can work around it with this nasty little hack:
try {
} catch (e) {
/*istanbul ignore next*/
(function() {
//Your code here will be ignored..
}());
}
it's okay, i can wait :) great to see this repo more active!
great to see this repo more active!
:+1: on this
Any update on this?
@gotwarlost any update on this?
btw, this is also working:
try {
} catch (err) /* istanbul ignore next */ {
}
That doesn't work with transpiled async functions though.
Alternatively, you can wrap your catch in another block
try {
stuff()
} catch (err) {
/* istanbul ignore next */ {
console.error(err)
throw err
}
}
in newer versions of JS there are block scopes, see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block
maybe try that. A block scope, is very similar to an anonymous IIFE
(function(){
// similar to below
});
{
// this block scope is similar to above
}
In order to perform a test it should not be needed to change your code (unless it's an improvement to the code itself). However, it seems to me that the following works and the code structure is intact.
try {
...
} catch(err) /* istanbul ignore next */
{
...
}
The new-line after the ignore comment does the trick.
Grumble... @xtvdata 's trick might work but tslint --fix always gets rid of, for sound reasons I don't want to disable elsewhere, and disabling just here makes a mess.....
For what it's worth, this is working for me (including without the newline described above):
try {
...
} catch (error) /* istanbul ignore next */ {
...
}
The entire contents of the catch block are being ignored as expected.
Not sure if this was fixed recently, but it seems this issue can probably be closed now.
Another issue, how to make it work with Typescript? If I try the fix of @bitpay, it does not work with transpiled code to commonjs and es5.
In a promise catch block I had to do this:
.catch(err => /* istanbul ignore next */ {
cLogger.error("error occurred during processing that was not caught elsewhere", err);
callback(err);
});
It's a little goofy, I'd expect to be able to do:
/* istanbul ignore next */
.catch(err => {
cLogger.error("error occurred during processing that was not caught elsewhere", err);
callback(err);
});
but that did not work, nor did putting the ignore directive after the curly brace on the catch line.