Code Coverage for Istanbul Incorrect when using Sandbox in Nodeunit

I wrote a bunch of tests using nodeunit to test my code. In doing so, I wanted to make fun of the modules required by the test code. Instead of changing the code to make it more easily verifiable with mocks, inverting the control when it was not necessary, I used the sandunits sandbox function instead.

Example

var nodeunit = require("nodeunit"); export.MyTest = { test1(test) { var fakeGlobals = { require: function(filename) { if (filename == "CoolUtil.js") { return { doit: function wasCool() { return true; } }; } else { return require(filename); } } }; var testSubject = nodeunit.utils.sandbox("ModuleUnderTest.js", fakeGlobals); test.equals(42, testSubject.doSomethingCoolUsingCoolUtil(), "Worked"); test.done(); } } 

Istanbul gives me the wrong coverage report numbers. I tried using the -post-require-hook flag, which is said to be used with RequireJS, which I'm fine with switching, but haven't recognized yet.

test / node_modules /. bin / istanbul cover --v --hook-run-in-context -root test / node_modules /. bin / nodeunit - --reporter junit --out target target / results / unit_tests test

Has anyone been successful with nodeunit, istanbul and using the sandbox function in nodeunit?

+6
source share

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


All Articles