Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda