istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

how to ignore a catch statement?

Open jonathanong opened this issue 9 years ago • 18 comments

this does not work:

try {

} /* istanbul ignore next */ catch (err) {

}

jonathanong avatar May 12 '15 17:05 jonathanong

You have to move it into the catch to ignore the catch:

try {

} catch (e) {
    /*istanbul ignore next*/
}

davglass avatar May 12 '15 17:05 davglass

i want to ignore the entire block though, not the first statement in the block

jonathanong avatar May 12 '15 17:05 jonathanong

Yeah, this is not good. Needs fixing in the instrumenter

gotwarlost avatar May 12 '15 17:05 gotwarlost

Looks like CatchClause is included in the syntax but not in the Walker. So it's not being parsed.

davglass avatar May 12 '15 17:05 davglass

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.

gotwarlost avatar May 12 '15 18:05 gotwarlost

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..
  }());
}

davglass avatar May 12 '15 18:05 davglass

it's okay, i can wait :) great to see this repo more active!

jonathanong avatar May 12 '15 18:05 jonathanong

great to see this repo more active!

:+1: on this

dougwilson avatar May 12 '15 21:05 dougwilson

Any update on this?

luchillo17 avatar Feb 25 '17 17:02 luchillo17

@gotwarlost any update on this?

btw, this is also working:

try {

} catch (err) /* istanbul ignore next */ {

}

Ondama avatar Mar 02 '17 13:03 Ondama

That doesn't work with transpiled async functions though.

battlesnake avatar Jul 13 '17 16:07 battlesnake

Alternatively, you can wrap your catch in another block

  try {
    stuff()
  } catch (err) {
    /* istanbul ignore next */ {
      console.error(err)
      throw err
    }
  }

acarl005 avatar Aug 09 '17 22:08 acarl005

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
}

ORESoftware avatar Aug 11 '17 00:08 ORESoftware

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.

xtvdata avatar Feb 15 '18 09:02 xtvdata

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

shaunc avatar Apr 12 '18 01:04 shaunc

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.

bitjson avatar Mar 28 '20 06:03 bitjson

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.

euberdeveloper avatar Jan 04 '21 13:01 euberdeveloper

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.

jcollum avatar Jan 26 '21 22:01 jcollum