I just started with an example file (example.js) which has the following statements:
x = 42;
if(false)
x = -1;
I don't have any unit test file. but when I run
istanbul cover example.js
I see some the below coverage data
=============================== Coverage summary ===============================
Statements : 66.67% ( 2/3 )
Branches : 50% ( 1/2 )
Functions : 100% ( 0/0 )
Lines : 66.67% ( 2/3 )
==============================================================================
Does that imply I have 2 statements out of 3 that are covered? If so, how can it report that this code is covered when I don't have any unit test file targeting that code?
From what I understand by definition of code coverage, it code that is covered by unit tests. I don't understand why istabul says that I have 2 statements covered when I have not written any unit tests.
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.
For a test set to achieve 100% branch coverage, every branching point in the code must have been taken in each direction, at least once. The archetypical example, showing that 100% statement coverage does not imply 100% branch coverage, was already given by Alexey Frunze.
Yet another code coverage tool for Javascript, with the following features: All-javascript instrumentation library that tracks statement, branch, and function coverage and reverse-engineers line coverage with 100% fidelity.
Istanbul checks to see what lines of code were run. Simply running the file will execute two of the lines, and ignoring the third. If you were to create something like this:
module.exports = {
myFunc: function() {
x = 42;
if(false) {
x = -1;
}
}
};
You'd get a different result (x = 42
wouldn't be run at all). The one statement executed is module.exports
=============================== Coverage summary ===============================
Statements : 25% ( 1/4 )
Branches : 0% ( 0/2 )
Functions : 0% ( 0/1 )
Lines : 25% ( 1/4 )
================================================================================
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