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

169
Vistas
Stubbing two methods exported from the same file using Sinon

I'm a newbie to JavaScript and need help in unit-testing my code. There is an existing file F1.js whose methods I'm using in my implementation. The file looks like this:

F1.js :


function A({p1, p2}, callback) {

}

function B(q1, q2) {

}

module.exports = A;
module.exports.B = callbackify(B);

My file Sol.js uses these functions as,

Sol.js:


const A = require('/F1');
const {
  B 
} = require('/F1');


somefunction() {



   B( param1, param2, (err, result) => {
      A( {anotherParam, result}, 
         function (err, result2) {
             // do something with result2

         }
      ) 
   })

}

I'm trying to unit test the method somefunction and struggling with the stubbing. Here is how I mock the functions

Sol_Test.js

B_stub = sinon
      .stub()
      .callsFake((param1, param2, callback) => callback(null, result1));

A_stub = sinon
      .stub()
      .callsFake(({ param1, param2 }, cb) => cb(null, result2));

return proxyquire(
   'F1' : {
        A_stub,
        B : B_stub,
   }
)

I'm getting the error : B is not a function

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Found the solution and sharing it here in case someone else needs it :D

Modified Sol.js as below

const A = require('/F1');


somefunction() {



   A.B( param1, param2, (err, result) => {
      A( {anotherParam, result}, 
         function (err, result2) {
             // do something with result2

         }
      ) 
   })

}

Changed the stubbing in unit test :

B_stub = sinon
      .stub()
      .callsFake((param1, param2, callback) => callback(null, result1));

A_stub = sinon
      .stub()
      .callsFake(({ param1, param2 }, cb) => cb(null, result2));

A_stub.B = B_stub;

return proxyquire(
     '/F1': A_stub,
)
about 4 years ago · Juan Pablo Isaza Denunciar
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