Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda