Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!