Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest coverage missing only the if condition line in a weird way - why and how to fix

I have a index.js which looks like

...
[line 69] if A:
[line 70]    foo()
[line 71] else if B:
[line 72]    bar()

I have written a test for line 69 - 70, the evidence is if I comment it out, the yarn test --coverage will show line 69, 70 missing.

However, even with this test, the coverage complains line 69 is missing

Another thing I noticed is that if I swap the if conditions to

...
[line 69] if B:
[line 70]    bar()
[line 71] else if A:
[line 72]    foo()

It is still line 69 missing in coverage

What is the reason for this in jest? How can I fix that?

like image 905
Suicide Bunny Avatar asked Sep 04 '25 01:09

Suicide Bunny


1 Answers

There are really three branches here: either if gets triggered, or else if, or neither.

Even though the latter case is empty in this case (as there is no else statement) you still need a test where neither if or else if gets triggered for full coverage.

like image 54
Marcin Avatar answered Sep 06 '25 19:09

Marcin