See also #230.

You can work around it with some extra braces:

I'm having this same issue, it seems like a problem with the branch coverage though because as the coverage shows the 'else if' case is covered by a unit test. It's very confusing to get the 'else path not taken' marking when the 'else if' is clearly covered as shown by the 11x marking on the right side.
Just found this issue when running into the same problem. I solved it by adding the ignore line above the first if, like so:
```
/* istanbul ignore else */
if (element) {
element.addEventListener('animationend', this.handleAnimationEnd)
} else if (this.contentElement) {
this.contentElement.removeEventListener('animationend', this.handleAnimationEnd)
}
@woutervanvliet Yeah but then the first else is likely ignored too when we want to know whether that first else was taken! We only want to ignore the last one.
Bump.
This is annoying. I have a list of if..else if.. statements that does not need an else clause, because it's impossible to get anything else other than what's accounted for. Similar problem if I use a switch statement.
I've confirmed that if I put the /* istanbul ignore else */ above the first if, it ignores all the else if clauses, not just the final if.
My annoyingly bad choices are:
/* istanbul ignore else */ above the first if, and lose the benefit of Istanbul tracking my coverage of all my else if statements.else conditionrefactor the if...else if... series like this:
if (one) { .. }
else if (two) { .. }
else if (three) { .. }
else {
/* istanbul ignore else */
if (four) { .. }
}
I picked option (4), but it's extra annoying because not only do I have to contort the code in that weirder shape, but then my linter complains about the "lonely if", so I have to put another comment to get the linter to hush. :/
Something like /* instanbul ignore-final-else */ would be pretty useful for stuff like this.
I'd actually simplify the choices we have to the following:
So is there any solution to this? I agree @getify that the workaround isn't working for all situations.
this works
if (condition1) {
} else /* istanbul ignore next */
if (condition) {
Didn't mean that we should use this, just because it works.
Can someone explain to me why the solution of @livingston should not be done?. it worked for me.
@arleyyap this "suggested solution" doesn't fit the form of the problem I brought up (which differs from the OP)... consider:
if (condition1) {
// ..
} /* istanbul ignore next */
else {
// ..
}
vs
if (condition1) {
// ..
} else /* istanbul ignore next */
if (whatShouldGoHere) {
// ..
}
In the first snippet, we're trying to ignore an else... which doesn't work -- that's my complaint. In the second snippet, the ignoring is of an if that's part of an else... but what condition should go there for an else that isn't ever planned to fire?
This hack could "work", but IMO is as janky (or worse) than some other work-arounds discussed earlier in the thread:
if (condition1) {
// ..
} else /* istanbul ignore next */
if (false) {
// ..
}
I am also facing the same issue while running test case with only if statement.
Even '/* istanbul ignore else */' statement doesn't ignore else case and results branch coverage less.
The /* istanbul ignore else */ should only affect it's own level and not affect nested if else statements.
Definitely still a problem in [email protected] and [email protected]. In fact, else if statements cause endless problems for both ignore if and ignore else... example:
/* istanbul ignore if: this can cover branch 1 */
if (false) {
console.log("branch 1");
} /* istanbul ignore if: does not work */ else /* istanbul ignore if: not here either */ if (false) {
console.log("branch 2");
} else if (false) {
console.log("branch 3");
}
As soon as you need to ignore an if entry in a series of else if chains, you can never ignore another one. Your only option is to use an ignore next -- but that actually ignores the _entire rest_ of the chain, including branch 3.
(I realize this is slightly different than the ignore else case in the OP, but I think it's part of the larger problem with else-if chains.)
EDIT: I guess my example could be solved by putting /* istanbul ignore next*/ _inside_ each if block, which isn't bad, whereas there is no easy solution for the else case. So maybe my complaint is lower priority.
Most helpful comment
Bump.
This is annoying. I have a list of
if..else if..statements that does not need anelseclause, because it's impossible to get anything else other than what's accounted for. Similar problem if I use aswitchstatement.I've confirmed that if I put the
/* istanbul ignore else */above the firstif, it ignores all theelse ifclauses, not just the finalif.My annoyingly bad choices are:
/* istanbul ignore else */above the firstif, and lose the benefit of Istanbul tracking my coverage of all myelse ifstatements.elseconditionrefactor the
if...else if...series like this:I picked option (4), but it's extra annoying because not only do I have to contort the code in that weirder shape, but then my linter complains about the "lonely if", so I have to put another comment to get the linter to hush. :/