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

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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