myutil.ts
const text = require('randtext'); // library to generate random text (example)
export const myUtil = async (myId: string):Promise<boolean> =>{
console.info(text.generateText());
return true;
};
myutil.test.ts (this is inside a test folder - so 1 folder layer down)
const proxyquire = require('proxyquire');
const sinon = require('sinon);
describe("util tests",() =>{
let fakeStub = sinon.stub();
const fakehandler = proxyquire('../myUtil.ts',{
'randtext': {
generateText: function(){ console.info("i did not get called"); return fakeStub;}
},
});
afterEach(() => {fakeStub.reset();});
beforeEach( () => {fakeStub = sinon.stub();});
it('my test', async () =>{
fakeStub.resolves(() => { return "my hardcoded text";});
expect ( await fakehandler.myUtil("111")).toBe(true);
}
});
Test case prints - text generated by generateText and NOT the hardcoded string "my hardcoded text". "i did not get called" is NEVER printed. In short, its only executing the older dependencies and not the newer one. Stuck on this for long. Any idea why this is happening ?
Tried changing require to import
Expected console output is - "my hardcoded text"