I\'m trying to use the following mock:
const mockLogger = jest.fn();
jest.mock(\"./myLoggerFactory\", () => (type) => mockLogger);
but m
The problem is that jest.mock
are hoisted to at the begin of the file on run time so const mockLogger = jest.fn();
is run afterwards.
To get it work you have to mock first, then import the module and set the real implementation of the spy:
//mock the module with the spy
jest.mock("./myLoggerFactory", jest.fn());
// import the mocked module
import logger from "./myLoggerFactory"
const mockLogger = jest.fn();
//that the real implementation of the mocked module
logger.mockImplementation(() => (type) => mockLogger)