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

220
Vistas
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 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