I'm using Node with TypeScript, TypeDI and Jest. I'm creating services that depend on each other, let's say:
@Service()
export class MainService{
constructor(private secondService: SecondService){}
public someMethod(someString: string) // implementation depends on secondService
}
@Service()
export class SecondService{
constructor(private thirdService: ThirdService){}
}
@Service()
export class ThirdService{
constructor(){}
}
I want to test MainService, but to instantiate it I need to pass dependency and that dependency needs another dependecy. I tried to do this like, it works, but is ugly:
const secondService = new SecondService(new ThirdService());
jest
.spyOn(secondService, "someMethod")
.mockImplementation((someString: string) => {
// do something
return something;
});
const mainService = new MainService(secondService);
// use mainService in tests
Of course creating new instance of dependency is not always an option, and defienetly not an option when it has many dependencies.
I think it should look more like:
const secondService = SomeMockFactory.create(SecondService);
but i can't find any way to create mock while cutting off dependencies. I tried using
const secondService = jest.genMockFromModule("path/to/second/service");
but after trying to spyOn secondService methods TS is throwing error that "someMethod" is not a function. What am I missing / doing wrong? Do I need some other library than Jest?