I am trying unsuccessfully to mock a Promise:
utils.js
export const fetching = () => {
const data = [1,2,3];
return new Promise(resolve=>resolve(data));
}
MyComponent
export function MyComponent() {
...
useEffect(()=>{
async function callback() {
const fetchedData = await fetching();
}
callback();
}, []);
return (
...
);
}
MyTest
import * as myStuff from ...;
jest.spyOn(myStuff, 'fetching').mockImplementation(()=>Promise.resolve([5]))
test('should ...', async () => {
// given
render(<MyComponent/>);
});
What am I getting as fetchedData by running the above test is undefined instead of [5]. Any idea why?