View on GitHub

fetch-mock

Mock http requests made using fetch

Installation

Install fetch-mock using npm install --save-dev fetch-mock

In most environments use const fetchMock = require('fetch-mock') to use it in your code. Some exceptions include:

Global fetch

By default fetch-mock assumes fetch is a global so once you’ve required fetch-mock refer to the quickstart and api docs.

Polyfilling fetch

Many older browsers will require polyfilling the fetch global

Non-global fetch

When using a non-global fetch implementation (such as node-fetch or fetch-ponyfill) use the sandbox() method to return a function that can be used as a replacement for fetch, and be passed into your source code using your choice of mocking library. The function returned by sandbox() supports the full fetch-mock api so once generated it can be worked with as if it were the original fetch-mock object, e.g.

const fetchMock = require('fetch-mock');
const myMock = fetchMock.sandbox().mock('/home', 200);
// pass myMock in to your application code, instead of fetch, run it, then...
expect(myMock.called('/home')).to.be.true;

References to Request, Response, Headers, fetch and Promise

If you’re using a non-global fetch implementation, or wish to use a custom Promise implementation, you may need to tell fetch-mock to use these when matching requests and returning responses. Do this by setting these properties on fetchMock.config, e.g

const ponyfill = require('fetch-ponyfill')();
fetchMock.config = Object.assign(fetchMock.config, {
    Promise: require('Bluebird').promise,
    Headers: ponyfill.Headers,
    Request: ponyfill.Headers,
    Response: ponyfill.Response
    fetch: ponyfill
}, 

This should be done before running any tests.

Note that when using node-fetch, fetch-mock will use the instance you already have installed so there should be no need to set any of the above (apart from fetch, which is required if you intend to use the .spy() method)