I am using React with Typescript and in a unit test I have to mock a module which is included inside the main file to which I am writing unit tests for (a container component).
The file I am trying to test imports this module:
import PerformancesResultsReader from './../../models/PerformancesResultsReader';
and then it uses the module in the following way:
const performancesResultsReader = new PerformancesResultsReader();
performancesResultsReader.read();
This file is called AppPage.component.tsx
The test file is located in the same folder and when I use the automatic mock, it works as expected. The automatic mock is implemented simply through the jest mock
of the same import:
jest.mock('./../../models/PerformancesResultsReader');
In this way all the methods simply return undefined
.
Now I was trying to add a manual mock instead of the automatic one.
This is the PerformancesResultsReader.js
mock code:
console.log('including mock!');
const mock = jest.fn().mockImplementation(() => {
return {
read: () => {
console.log('mocked');
}
};
});
export default mock;
I tried to place it inside a __mocks__
sub folder in the same level of the file I am testing, and I tried to put it also in the same folder of the imported module I want to mock.
In both cases it doesn't seem to be called at all (the first console log never gets printed).
Any idea about what I am doing wrong please?