I'm facing an issue while trying to mock a function of an object using Jest and Typescript. Below is a concise version of my code:
// myModule.ts
export const Foo = {
doSomething: () => {
// ... does something
console.log('original implementation');
}
}
Here's how I attempted to write the test:
jest.mock('.myModule.ts', () => {
return {
Foo: {
doSomething: jest.fn().mockImplementation(() => {
console.log('mock implementation')
})
}
}
})
// .. moving forward in the test
Foo.doSomething();
After writing this, I expected
console.log('mock implementation')
to be triggered when calling Foo.doSomething()
. However, it doesn't throw any errors, stops executing the original implementation, but also fails to run my mockImplementation.