Understanding Code Coverage in Istanbul

I just started with an example file (example.js) that has the following statements:

x = 42; if(false) x = -1; 

I do not have a unit test file. but when i started

 istanbul cover example.js 

I see some coverage data below

 =============================== Coverage summary =============================== Statements : 66.67% ( 2/3 ) Branches : 50% ( 1/2 ) Functions : 100% ( 0/0 ) Lines : 66.67% ( 2/3 ) ============================================================================== 

Does this mean that I have 2 of 3 applications that are covered? If so, how can he report that this code is distributed when I don’t have any unit test file aimed at this code?

From what I understand by definition of code coverage, this is code that is covered by unit tests. I do not understand why istabul says that I have 2 statements if I have not written any unit tests.

+6
source share
2 answers

Istanbul checks which lines of code have been run. Simple file execution will execute two lines and ignore the third. If you created something like this:

 module.exports = { myFunc: function() { x = 42; if(false) { x = -1; } } }; 

You will get a different result ( x = 42 will not start at all). One statement is module.exports : module.exports

 =============================== Coverage summary =============================== Statements : 25% ( 1/4 ) Branches : 0% ( 0/2 ) Functions : 0% ( 0/1 ) Lines : 25% ( 1/4 ) ================================================================================ 
+5
source

example.js suppose this is a test file. And if you want to include the source code, run with --include-all-sources .

+1
source

Source: https://habr.com/ru/post/969552/


All Articles