Istanbul collects coverage by wrapping various functions inside the JavaScript language so that when your code is invoked, so too is Istanbul's monitoring code. By wrapping code the way Istanbul does, we are able to see coverage on a granular level, like which branches have been invoked.
The report shows the percentage of the code that has been executed or covered by tests. You can see the coverage result for classes, methods, and lines. Branch coverage shows the percentage of the executed branches in the source code (normally, these are the if / else and switch statements).
To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.
There are a number of coverage criteria, the main ones being:
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.xN
in left column is the amount of times that line has been executed.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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With