6

I have written a bunch of tests using nodeunit to test my code. In doing so I wanted to mock out modules being required by the code under test. Instead of changing the code to make it more easily testable with mocks, inversion of control, when it wasn't needed, I instead used nodeunits sandbox function.

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 is giving me the wrong coverage report numbers. I tried using the flag --post-require-hook which is said to be used with RequireJS, which I'm fine with switching to but haven't learned yet.

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

Has anybody been successful with nodeunit, istanbul and using the sandbox feature in nodeunit?

Erock 634
  • 591
  • 3
  • 18
  • How wrong are they ? Tried : https://github.com/SBoudrias/gulp-istanbul#includeuntested ? – Jscti Jan 25 '17 at 11:00

0 Answers0