I use Karma (currently v0.10.10) and Jasmine for my unit tests and Istanbul (via karma coverage) for code coverage reports. I noticed the weird behavior of the code coverage reporter in a particular case.
The code I'm trying to check is something like this:
var foo = function(element) { var callback = function() {
In my test, I send a custom input event to the element being tested and performs a callback function . The test checks the consequences of the callback and passes the test. In fact, even when I put the hairy console.log("foo") in the callback, I can clearly see that it is being printed. However , the Istanbul report erroneously indicates that the callback was not performed at all .
Changing the test code to use an anonymous function in the event listener callback eliminates the wrong behavior:
element.addEventListener("input", function() { callback(); });
However, I completely despise the βsolutionsβ that modify the application code to compensate for the lack of a code quality control tool.
Is there a way in which you can get the correct code coverage without wrapping the callback with an anonymous function?
javascript callback code-coverage karma-runner istanbul
mingos
source share