I'm trying to write a test for function foo() with jest. I want to be able to mock the data returned from getFoo()
Here is an example of a module where a function Foo() calls getFoo(), which returns some data.
---foo.js---
export function getFoo() {
//reach out to some API and get some data
return 'foo data';
}
export function foo() {
const data = getFoo();
return data;
}
Here is the test i'm trying to make.
---foo.test.js---
import { foo, getFoo } from '../app/utils/asdf';
jest.mock('../app/utils/asdf');
const getFooMock = jest.requireMock('../app/utils/asdf').getFoo as jest.Mock;
it('should return NOT foo', () => {
getFooMock.mockReturnValue('NOT foo');
expect(foo()).toBe('NOT foo');
});
Test outputs Expected: "NOT foo" Received: "foo data"
I have been pulling my hair out over this all day. How do i change the output of getFoo() from within my test??