I'm curious about why the final assertion (expect(msgSpy).toBeCalled()
) in this code snippet is failing. What adjustments should be made to ensure it passes?
it('spyOn test', () => {
const newClient = () => {
const getMsg = () => {
return 'dear spyOn';
};
const getHello = () => {
return `Hello, ${getMsg()}`;
};
return {
getMsg,
getHello,
};
};
const c = newClient();
const helloSpy = jest.spyOn(c, 'getHello');
const msgSpy = jest.spyOn(c, 'getMsg');
expect(c.getHello()).toEqual('Hello, dear spyOn');
expect(helloSpy).toBeCalled(); // evaluates to true
expect(msgSpy).toBeCalled(); // <-- evaluates to false!
});