Lets say I have the following named export in a file customer.ts
export const saveDetails = ()=>{}
export const loadDetails = ()=>{}
assuming I'm using this in another file
import {saveDetails, loadDetails} from './customer.ts'
I want to mock mock './customer.ts' with a custom implementation. To that end I use the following code
const mockSaveDetails = jest.fn().mockImplementation(() => {});
jest.mock('./customer.ts', () => {
return {
saveDetails: mockSaveDetails
};
});
Now when I run this code I get the following error
ReferenceError: Cannot access 'mockSaveDetails' before initialization
As per the documentation on https://jestjs.io/docs/en/es6-class-mocks I understand the mock is hoisted to the top and the exception is if the variable has the prefix mock
. So as per the documentation this should work right ? If not what is the alternative to providing a mock mock implementation and spying on those implementation (like see howmany calls were made to saveDetails
for example) with certain arguments.