How do I read an Istanbul Coverage Report?
@StijndeWitt: You asked
What I don't get is how it can give me 100% statement coverage, but less that 100% function converage on the same file. How can 100 percent of the statements have been covered if not even all functions were called?
The attached screenshot illustrates how Line Coverage can be 100%, but Function Coverage is less. Lines 4, 5, and 7 count as lines. The line containing the function definition (F1) at line 3 does not, but it obviously contains a function. Line 5 also contains a function (F2) and an additional statement. Therefor, in NYC's world of counting:
- lines = 3
- statements = 4 (of which 1 is not covered)
- functions = 2 (of which 1 is not covered)
Apparently, function headers do not count as 'lines' but they do count as a function. That explains why Line Coverage can be 100%, while Function Coverage (or Statement Coverage) is less.
Running istanbul should also produce an HTML file for the report (should be in the coverage folder). This HTML should give you drill-down information when you click on files/folders.
The percentage of functions covered is calculated by the number of functions that were called during tests, divided by total number of functions. Same goes for lines and statements (which will usually be close to each other unless you have very long statements).
Branches mean decision points like if-else
blocks. For example, say your code only contains one if-else
statement, and your tests only pass through the if
part but not the else
part, then your branches percentage should be 50%.
Hope that makes things clearer.
There are a number of coverage criteria, the main ones being:
- Function coverage Has each function (or subroutine) in the program been called?
- Statement coverage Has each statement in the program been executed?
- Branch coverage Has each branch (also called DD-path) of each control structure (such as in if and case statements) been executed? For example, given an if statement, have both the true and false branches been executed? Another way of saying this is, has every edge in the program been executed?
- Line coverage has each executable line in the source file been executed?
For each case, the percentage represents executed code vs not-executed code, which equals each fraction in percent format (e.g: 50% branches, 1/2).
In the file report:
'E'
stands for 'else path not taken', which means that for the marked if/else statement, the 'if' path has been tested but not the 'else'.'I'
stands for 'if path not taken', which is the opposite case: the 'if' hasn't been tested.- The
xN
in left column is the amount of times that line has been executed. - Not executed lines, or pieces of code, will be highlighted in red.
This has been verified for Istanbul v0.4.0, I'm not sure if this still applies for subsequent versions, but being that library is based on solid theoretic principles, behavior shouldn't change too much for newer versions.
It also provides some color codes -
Pink: statements not covered.
Orange: functions not covered.
Yellow: branches not covered.
Full Istanbul docs here:
https://istanbul.js.org
For more in-depth theory on code coverage:
https://en.wikipedia.org/wiki/Code_coverage
Hope it helps!