title: How to allow partial TypeScript types when mocking functions - Jest, TypeScript
body: <p>I have a function which I would like to mock for testing purposes in TypeScript. In my tests, all I care about are the <code>json</code> and the <code>status</code>. However, when using Jest's <code>jest.spyOn</code> the type of my mocked function is set to return a http <code>Response</code> type. This is awkward as it means I have to manually go and implement a bunch of functions and properties that are irrelevant and arbitrary.</p>
  
  <p>I suspect there is some way to use a partial type here to allow better and more useful mocking by overriding the return type to only that I care about. How would I go about doing this?</p>
  
  <pre><code>export function mockApi(json: object, status: number): void {
    jest.spyOn(
      myApiModule,
      'methodWhichReturnsAResponse'
    ).mockImplementation(() =&gt;
      Promise.resolve({
        json: () =&gt; Promise.resolve(json),
        status,
        // Below here is to appease jest types (not needed for
        // testing purposes at the time of writing)
        headers: {
          has: (name: string) =&gt; true,
          // get, set, etc...
        },
        ok: true,
        redirected: false,
        // and about 10 other properties which exist on the Response type
        // ...
      }),
    );
  }
  
  </code></pre>