gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 786 forks source link

Ignore implied "else" #946

Open tremby opened 2 years ago

tremby commented 2 years ago

I have a couple of if statements which if matched return. One of the two will return in all real-world cases so I want to ignore the fallback return statement.

I'm reading the docs at https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md and it looks like the following should do what I want:

if (diff > 0) return 1;
/* istanbul ignore else */
if (diff < 0) return -1;
return 0;

But I'm getting told that the return 0 line is not covered. If I instead use a /* instanbul ignore next */ just before the return 0 line, now it tells me that the if (diff < 0) line is uncovered.

I found that I need to include both ignore statements:

if (diff > 0) return 1;
/* istanbul ignore else */
if (diff < 0) return -1;
/* istanbul ignore next */
return 0;

Is this as intended?

I found I can also do this, but I don't like the code:

if (diff > 0) return 1;
/* istanbul ignore else */
if (diff < 0) return -1;
else return 0;