How do I test with jest whether this api middleware handles errors thrown by fetch?
<p>I have the following api middleware in my react/redux app, which I'm unittesting with jest. The module code is:</p>

<pre class="lang-js prettyprint-override"><code>// ./src/middleware/api.js

import fetch from 'isomorphic-fetch';

// Checks if the returned statuscode is in the successful range
const handleErrors = (response) =&gt; {
  if (!response.ok) {
    throw Error(response.statusText);
  }
  return response;
};

export const get = (endpoint) =&gt; {
  const options = { credentials: 'include', method: 'GET' };

  return fetch(endpoint, options)
    .then(handleErrors)
    .then(response =&gt; response.json())
    .catch(error =&gt; error.message);
};
</code></pre>

<p>And the test is:</p>

<pre class="lang-js prettyprint-override"><code>// ./src/middleware/__tests__/api.test.js

import fetch from 'isomorphic-fetch';
import { get } from '../api';

// Mock calls to fetch by the api middleware
// loads https://github.com/jefflau/jest-fetch-mock instead of fetch
jest.mock('isomorphic-fetch');

describe('api middleware', () =&gt; {
  describe('get', () =&gt; {
    it('should return the response on success', () =&gt; {
      const expected = { data: ['data'], meta: {} };
      const body = JSON.stringify(expected);
      const init = { status: 200, statusText: 'OK' };

      fetch.mockResponseOnce(body, init);

      return get('http://endpoint').then(actual =&gt; expect(actual).toEqual(expected));
    });

    it('should return the statusText for unsuccessful status codes', () =&gt; {
      const expected = 'Unauthorized';
      const body = JSON.stringify({ errors: ['You are not logged in'] });
      const init = { status: 401, statusText: expected };

      fetch.mockResponseOnce(body, init);

      return get('http://endpoint').then(actual =&gt; expect(actual).toEqual(expected));
    });

    // I have not been able to mock this so far    
    it('handles fetch errors', () =&gt; {
      return get('doesnotexist').then(actual =&gt; expect(actual).toEqual(false));
    });
  });
});
</code></pre>

<p>In the last assertion i want to test whether it catches errors thrown by fetch (so when there is no network connection for example). But I'm having trouble testing that. I'm not sure how I'd simulate an error in fetch and then test for it. Does anyone know how I would go about mocking the last assertion?</p>