gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 787 forks source link

Code Coverage reported as less than 100% with no line numbers #898

Open spuy767 opened 5 years ago

spuy767 commented 5 years ago

code coverage error

I know this isn't much to go on, but I've rewritten most of the unit tests for the page in question and it seems to have no effect. I'm just trying to see if anyone else has had a similar error and found a solution that might point me in the right direction.

sveyret commented 4 years ago

I've got the same problem with no solution… :disappointed:

spuy767 commented 4 years ago

I discovered long after I wrote this post that its related to anonymous functions. If you create the function and export it, you can pull it into your spec file and test it. Then just use the function by name in your callback.

sveyret commented 4 years ago

Thank you for the information @spuy767. Don't know if it's the same for me, I believe that all branches are tested even in the anonymous functions. Strangely, nyc reports a 99% coverage on my computer, but coveralls.io reports 100%… If you want to have a look, this is the project https://github.com/slune-org/confinode

sveyret commented 4 years ago

Got it… Indeed, as @spuy767 said, I had an anonymous less-than-single-line callback which was not called in my tests. So:

That would be great if istanbul could give a hint about the concerned line number…

georgeben commented 4 years ago

Please I don't understand, can you explain how you fixed the issue?

sveyret commented 4 years ago

It was not an issue actually, but in my code, I had something like that:

fn(cb => somecode())

The function fn is executed, but for some reason, it does not call the callback. So the line is executed, there is no missing branch, but there is still some non-executed code in the line.

Note that if you want to detect where the problem is, you may try to spread your callback into more than 1 line (just a guess, not tested):

fn(cb =>
  somecode()
)

So that there is now a line which is not executed and istanbul can report its number.

spuy767 commented 4 years ago

Please I don't understand, can you explain how you fixed the issue?

The issue is more underlying with JavaScript. Inline callbacks are anonymous. If we assign them to a constant, and then pass them in, they will have names but still the scope of an anonymous function. To test the callback, I export it and import it into the test file. This is the only surefire way I’ve found to cover callback functions. In the test script, I’m using jest, I will just use a jest.fn() as the callback to make sure it’s called with the parameters I expect.