My test function is as follows:
const testLibrary = require("./test");
describe("Top Test", function() {
it("should test function", function(done) {
testLibrary.print();
done();
});
});
test.ts has the following two functions:
export function stubMe() {
console.log("original function");
}
export function print() {
stubMe();
}
When I run the test, it prints: 'original function'
I now try to stub my test as follows:
const testLibrary = require("./test");
const sinon = require("sinon");
const stubMe = sinon.stub(testLibrary, "stubMe");
stubMe.yields();
describe("Top Test", function() {
it("should test function", function(done) {
testLibrary.print();
done();
});
});
My test function still prints 'original function' indicating that the function hasn't been stubbed.
How do I stub the stubMe function?
Update:
I have modified my code based on Ankit's solution below to:
const sinon = require("sinon");
const testLibrary = require("./test");
testLibrary.stubMe();
const stubMe = new sinon.stub(testLibrary, "stubMe").callsFake(function () {
console.log("console from stub");
});
testLibrary.stubMe();
describe("Top Test", function () {
it("should test function", function (done) {
testLibrary.print();
done();
});
});
Oddly, this prints:
original function
console from stub
Top Test
original function
Why does the stub revert during the test?