Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How to stub IIFE module

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!