title: Mocking node-fetch with jest creating a Response Object for mocking
body: <p>I am trying to create Response Objects for mocking with jest, I can't seem to get the right syntax.</p>
  
  <p>Initialization,</p>
  
  <pre class="lang-js prettyprint-override"><code>jest.mock('node-fetch')
  const fetch = require('node-fetch')
  const { Response, Headers } = jest.requireActual('node-fetch')
  
  // Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
  const meta = {
    'Content-Type': 'application/json',
    'Accept': '*/*',
    'Breaking-Bad': '&lt;3'
  }
  // You can in fact use any iterable objects, like a Map or even another Headers
  const headers = new Headers(meta)
  const copyOfHeaders = new Headers(headers)
  
  const ResponseInit = {
    status: 200,
    statusText: 'fail',
    headers: headers
  }
  </code></pre>
  
  <p>With a basic test</p>
  
  <pre class="lang-js prettyprint-override"><code>  test('Basic Test', async () =&gt; {
      const token = ''
      const getDocList = new Response(JSON.stringify(downloadDocumentData), ResponseInit)
      fetch.mockResolvedValueOnce(Promise.resolve(getDocList))
      await module.doSomething('mock', token)
        .then( async(res) =&gt; {
          await expect(res.data).toEqual(Object)
        })
    }, 5000)
  </code></pre>
  
  <p>I'm getting an error which is</p>
  
  <pre><code>     FetchError {
          message:
           'invalid json response body at  reason: Unexpected token H in JSON at position 2',
          type: 'invalid-json' }
  </code></pre>
  
  <p>How can I initial a response for valid json, I have tried a lot of different things.</p>
  
  <p>Following the article at <a href="https://jestjs.io/docs/en/bypassing-module-mocks" rel="noreferrer">https://jestjs.io/docs/en/bypassing-module-mocks</a> but I want to return and test json instead.</p>