I am testing a class DogService.ts
. But DogService
has a private dependency called CatService
that gets new
'ed up in the constructor. I can successfully mock the dependency using:
jest.mock('../CatService', () => {
return {
CatService: jest.fn().mockImplementation(() => {
return {
someFunction: (async (someArg) => {
return await someAsyncMethod();
})
}
})
}
});
And it gets injected fine in the test:
describe("", () => {
it("", async () => {
const dogService = new DogService();
...
});
});
There's no import for the CatService
in my jest test file. The mock I guess is the import. However, I want to swap out the implementation of CatService
in different tests. I have tried jest.resetModules()
and a bunch of other things. Further, the above only seems to work when the mock is outside any tests. If I move it into the describe() function the real CatService
is used.
Any ideas how to do this? Most tutorials are for mocking classes or functions you directly use in the jest test, but obviously I am mocking a dependency.