title: How to strongly type jest mocks
body: <p>I would like to strongly type my jest mocks. To a certain extent, I can make it work but when a class has private properties I'm stuck.</p>
  
  <p>An additional question, when I use mocks (the way I currently do) the return type is of the original type but when I have to access any method added by Jest I have to typecast it so <code>jest.Mock</code> to access a method. Is there a better way to do this? I've tried to work with <code>jest.Mock</code>, <code>jest.Mocked</code>, <code>jest.MockInstance</code>.</p>
  
  <p>If anyone could point me in the right direction that would be great!</p>
  
  <pre class="lang-js prettyprint-override"><code>class MyTest {
      constructor(private readonly msg: string) {}
  
      public foo(): string {
          return this.msg;
      }
  }
  
  const myTestMock: jest.Mock&lt;MyTest, [string]&gt; = jest.fn&lt;MyTest, [string]&gt;(() =&gt; ({
      msg: 'private',
      foo: jest.fn().mockReturnValue('aaa'),
  }));
  // Results in error:
  // Type '{ msg: string; foo: Mock&lt;any, any&gt;; }' is not assignable to type 'MyTest'.
  // Property 'msg' is private in type 'MyTest' but not in type '{ msg: string; foo: Mock&lt;any, any&gt;; }'
  
  const myTestMockInstance: MyTest = new myTestMock('a');
  console.log(myTestMockInstance.foo()); // --&gt; aaa
  
  // Accessing jest mock methods:
  (&lt;jest.Mock&gt;myTestMockInstance).mockClear(); // &lt;-- can this be done without type casting
  </code></pre>
  
  <p>Dirty workaround:</p>
  
  <pre class="lang-js prettyprint-override"><code>const myTestMock: jest.Mock&lt;MyTest, [string]&gt; = jest.fn&lt;MyTest, [string]&gt;(
      // Cast to any to satisfy TS
      (): any =&gt; ({
          msg: 'private',
          foo: jest.fn().mockReturnValue('aaa'),
      })
  );
  </code></pre>