Cannot agree using jest.mock() instead of spyOn() (here) is a good solution. With that approach you have to mock all functions once at the top of your spec file.
But some of the test cases might need a different setup or stay real.
Anyway, I want to continue use spyOn().
A better way is to add following lines at the top of your spec file (right after the imports and before describe()).
import * as Foo from 'path/to/file';
jest.mock('path/to/file', () => {
return {
__esModule: true, // <----- this __esModule: true is important
...jest.requireActual('path/to/file')
};
});
...
//just do a normal spyOn() as you did before somewhere in your test:
jest.spyOn(Foo, 'fn');
P.S. Also could be an one-liner:
jest.mock('path/to/file', () => ({ __esModule: true, ...jest.requireActual('path/to/file') }));
P.P.S. 'path/to/file' to a constant might not work. at least at this moment of time for me it didn’t. So sadly you need to repeat the actual path 3 times.
Saving