I wonder why the last assert (expect(msgSpy).toBeCalled()
) in this example fails. What do I need to change to make it pass?
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(); // asserts to true
expect(msgSpy).toBeCalled(); // <-- asserts to false!
});