title: Mock fetch in Jest tests throwing “invalid json response” and “Unhandled promise rejection” errors
body: <p>I’m trying to do unit tests for some of my redux-saga generators and am getting a couple of errors I’m not sure how to resolve. I’m using <code>create-react-app</code>, so my testing suite is Jest and Enzyme.</p>
  
  <p>A basic example:</p>
  
  <p><strong>Setup:</strong> <code>src/setupTests.js</code></p>
  
  <pre><code>import 'jest-enzyme'
  import { configure } from 'enzyme'
  import Adapter from 'enzyme-adapter-react-16'
  
  global.fetch = require('jest-fetch-mock')
  
  configure({
      adapter: new Adapter(),
      automock: false,
      collectCoverageFrom: [
          '&lt;rootDir&gt;/src/**/*.js',
          '!&lt;rootDir&gt;/src/**/*.stories.js',
          '!&lt;rootDir&gt;/node_modules/',
          '!&lt;rootDir&gt;/src/index.js',
      ],
      coverageThreshold: {
          global: {
              branches: 90,
              functions: 90,
              lines: 90,
              statements: 90,
          },
      },
      verbose: true,
  })
  
  </code></pre>
  
  <p><strong>Saga:</strong> <code>src/store/sagas/api-saga.js</code></p>
  
  <pre><code>import { takeEvery, put } from 'redux-saga/effects'
  
  import {
      API_ERRORED,
      DATA_LOADED,
      DATA_REQUESTED,
  } from '../constants/action-types'
  
  export function* workerSaga() {
      try {
          const payload =
              yield fetch('https://jsonplaceholder.typicode.com/posts')
                  .then(response =&gt; response.json())
          yield put({
              type: DATA_LOADED,
              payload,
          })
      } catch (e) {
          yield put({
              type: API_ERRORED,
              payload: false,
          })
      }
  }
  
  export default function* watcherSaga() {
      yield takeEvery(
          DATA_REQUESTED,
          workerSaga,
      )
  }
  </code></pre>
  
  <p><strong>Saga Test:</strong> <code>src/store/sagas/api-saga.test.js</code></p>
  
  <pre><code>import { put, takeEvery } from 'redux-saga/effects'
  import watcherSaga, { workerSaga } from './api-saga'
  
  import {
      API_ERRORED,
      DATA_LOADED,
      DATA_REQUESTED,
  } from '../constants/action-types'
  
  describe('saga workers', () =&gt; {
      test('should dispatch action "DATA_LOADED" with result from fetch API',
          () =&gt; {
              const articles = 'Some content'
              const mockResponse = {
                  articles,
              }
              const generator = workerSaga()
  
              generator.next()
  
              expect(generator.next(mockResponse).value)
                  .toEqual(
                      put({
                          type: DATA_LOADED,
                          payload: {
                              articles,
                          },
                      })
                  )
  
              expect(generator.next().done).toBeTruthy()
          })
  })
  </code></pre>
  
  The errors I’m receiving:
  
  <pre class="lang-sh prettyprint-override"><code>(node:2009) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at undefined reason: Unexpected end of JSON input
  (node:2009) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
  (node:2009) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  (node:2009) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at undefined reason: Unexpected end of JSON input
  (node:2009) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
  </code></pre>
  
  <p>I thought introducing <code>jest-fetch-mock</code> would help resolve some of these issues, but it doesn’t seem so. The tests pass, but these errors persist.</p>
  
  <p>What am I missing?</p>
  
  <p><br /></p>
  
  <h3>Versions</h3>
  
  <p>redux@4.0.4<br />
  redux-saga@1.0.5<br />
  enzyme@3.10.0<br />
  jest@24.7.1</p>