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

219
Visualizações
Unit test private async method

You can test sync private functions using sinon spies:

// app
window.App = {
  const childFunc = function(){
    console.log("in childFunc")
  }

  const parentFunc = function(){
    this.childFunc()
  }

  const publicFuncs = function(){
      ...
      // Make private functions available foe testing
     _private = { 
        parentFunc: parentFunc,
        childFunc: childFunc
     }
  }

  return publicFuncs
}()

// tests
    const sandbox = sinon.createSandbox();
    const app = new window.App();

    beforeEach(function () {
      sandbox.spy(app._private);
    });

    afterEach(function () {
      sandbox.restore();
    });

    const app = new window.App();
    app._private.parentFunc()
    assert(app._private.childFunc.calledOnce)


What you're actually testing is that the key childFunc on app._private was called.

However, if parentFunc is called asyncronously, i.e.

  const otherFunc = async function(){
     await parentFunc()
  }  

Then the this keyword may not be defined inside parentFunc when it is run, which means that you cannot use this.childFunc.

You can call childFunc() directly, however sinon will not be able to detect that childFunc has been run, so you cannot test for it.

Am I missing anything here?

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