title: Mocking TypeDI service with Jest
body: <p>I'm using Node with TypeScript, TypeDI and Jest.
  I'm creating services that depend on each other, let's say:</p>
  
  <pre><code>@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(){}
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>const secondService = new SecondService(new ThirdService());
  jest
      .spyOn(secondService, "someMethod")
      .mockImplementation((someString: string) =&gt; {
          // do something
          return something;
      });
  const mainService = new MainService(secondService);
  // use mainService in tests
  </code></pre>
  
  <p>Of course creating new instance of dependency is not always an option, and defienetly not an option when it has many dependencies.</p>
  
  <p>I think it should look more like:</p>
  
  <pre><code>const secondService = SomeMockFactory.create(SecondService);
  </code></pre>
  
  <p>but i can't find any way to create mock while cutting off dependencies. I tried using </p>
  
  <pre><code>const secondService = jest.genMockFromModule("path/to/second/service");
  </code></pre>
  
  <p>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?</p>