Need private packages and team management tools?Check out npm Teams »


1.3.0 • Public • Published


Wrapper around fetch-mock - a comprehensive, isomorphic mock for the fetch api - which provides an interface that is more idiomatic when working in jest.

The example at the bottom of this readme demonstrates the intuitive API, but shows off only a fraction of fetch-mock's functionality. Features include:

  • mocks most of the fetch API spec, even advanced behaviours such as streaming and aborting
  • declarative matching for most aspects of a http request, including url, headers, body and query parameters
  • shorthands for the most commonly used features, such as matching a http method or matching one fetch only
  • support for delaying responses, or using your own async functions to define custom race conditions
  • can be used as a spy to observe real network requests
  • isomorphic, and supports either a global fetch instance or a locally required instanceg


npm install -D fetch-mock-jest

global fetch

const fetchMock = require('fetch-mock-jest')


jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox())
const fetchMock = require('node-fetch')


Setting up mocks

Please refer to the fetch-mock documentation

All jest methods for configuring mock functions are disabled as fetch-mock's own methods should always be used

Inspecting mocks

All the built in jest function inspection assertions can be used, e.g. expect(fetchMock).toHaveBeenCalledWith('').

fetchMock.mock.calls and fetchMock.mock.results are also exposed, giving access to manually inspect the calls.

The following custom jest expectation methods, proxying through to fetch-mock's inspection methods are also available. They can all be prefixed with the .not helper for negative assertions.

  • expect(fetchMock).toHaveFetched(filter, options)
  • expect(fetchMock).toHaveLastFetched(filter, options)
  • expect(fetchMock).toHaveNthFetched(n, filter, options)
  • expect(fetchMock).toHaveFetchedTimes(n, filter, options)
  • expect(fetchMock).toBeDone(filter)


  • filter and options are the same as those used by fetch-mock's inspection methods
  • The obove methods can have Fetched replaced by any of the following verbs to scope to a particular method: + Got + Posted + Put + Deleted + FetchedHead + Patched

e.g. expect(fetchMock).toHaveLastPatched(filter, options)

Tearing down mocks

fetchMock.mockClear() can be used to reset the call history

fetchMock.mockReset() can be used to remove all configured mocks

Please report any bugs in resetting mocks on the issues board


const fetchMock = require('fetch-mock-jest');
const userManager = require('../src/user-manager');
test(async () => {
    const users = [{ name: 'bob' }];
        .get('', users)
        .post('', (url, options) => {
            if (typeof === 'string') {
                return 202;
            return 400;
                url: ''
    expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
    await userManager.create({ name: true });
            url: '',
            body: { name: true }
    expect(await userManager.getAll()).toEqual([{ name: 'bob' }]);
    await userManager.create({ name: 'sarah' });
            url: '',
            body: { name: 'sarah' }
    expect(await userManager.getAll()).toEqual([
        { name: 'bob' },
        { name: 'sarah' }


npm i fetch-mock-jest

DownloadsWeekly Downloads






Unpacked Size

63.7 kB

Total Files


Last publish


  • avatar