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
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,
)