I am running Node v16.11.0. Trying to test a Worker module that I defined as such:
const Worker = (function(){
function start(){...}
function stop(){...}
function privateFunction1(){
...some stuff
...calls privateFunction2();
}
function privateFunction2(){...}
return {
start: start,
stop: stop,
/** for testing only:**/
_privateFunction1: privateFunction1,
_privateFunction2: privateFunction2
}
})();
module.exports = Worker;
And in my test file I have:
const worker = require("../../worker");
const sinon = require("sinon");
const chai = require("chai");
var assert = chai.assert;
var expect = chai.expect;
describe("Worker", function () {
const sandbox = sinon.createSandbox();
describe("#privateFunction1", function () {
.....
beforeEach(function () {
removeStub = sandbox.stub(worker, "_privateFunction2");
.......
});
afterEach(function () {
sandbox.restore();
});
it("should test something", function () {
let obj = {} //just for demo purposes
assert.equal(worker._privateFunction1(obj), false);
});
The expectation here is that _privateFunction1() will start executing, will call the privateFunction2 and that call will be handled by the stub.
But the real method gets called instead. Stubbing doesn't work.
Anyone has any ideas what is missing here?
Thanks