View on GitHub


Mock http requests made using fetch

API Documentation

Mocking calls to fetch

mock(matcher, response, options) or mock(options)

Replaces fetch with a stub which records its calls, grouped by route, and optionally returns a mocked Response object or passes the call through to fetch(). Calls to .mock() can be chained. Note that once mocked, fetch will error on any unmatched calls. Use .spy() or .catch() to handle unmocked calls more gracefully


This returns a drop-in mock for fetch which can be passed to other mocking libraries. It implements the full fetch-mock api and maintains its own state independent of other instances, so tests can be run in parallel. e.g.


Shorthand for mock() which limits to being called one time only. (see repeat option above)

get(), post(), put(), delete(), head(), patch()

Shorthands for mock() restricted to a particular method Tip: if you use some other method a lot you can easily define your own shorthands e.g.:

fetchMock.purge = function (matcher, response, options) {
    return this.mock(matcher, response, Object.assign({}, options, {method: 'PURGE'}));

getOnce(), postOnce(), putOnce(), deleteOnce(), headOnce(), patchOnce()

Shorthands for mock() restricted to a particular method and that will only respond once


This is used to define how to respond to calls to fetch that don’t match any of the defined mocks. It accepts the same types of response as a normal call to .mock(). It can also take an arbitrary function to completely customise behaviour of unmatched calls. It is chainable and can be called before or after other calls to .mock(). If .catch() is called without any parameters then every unmatched call will receive a 200 response


Similar to catch(), this records the call history of unmatched calls, but instead of responding with a stubbed response, the request is passed through to native fetch() and is allowed to communicate over the network.

To use .spy() on a sandboxed fetchMock, fetchMock.config.fetch must be set to a reference to the fetch implementation you use in your code.

    fetchMock.sandbox().mock('', 200)

Existing sandboxed fetchMocks can also have .sandbox() called on them, thus building mocks that inherit some settings from a parent mock


Chainable method that restores fetch() to its unstubbed state and clears all data recorded for its calls.


Chainable method that clears all data recorded for fetch()’s calls. It will not restore fetch to its default implementation

Note that restore() and reset() are both bound to fetchMock, and can be used directly as callbacks e.g. afterEach(fetchMock.restore) will work just fine. There is no need for afterEach(function () {fetchMock.restore()})

Inspecting how fetch() has been called


Most of the methods below accept two parameters, (filter, method)

called(filter, method)

Returns a Boolean indicating whether fetch was called and a route was matched. If filter is specified it only returns true if that particular route was matched.

done(filter, method)

Returns a Boolean indicating whether fetch was called the expected number of times (or at least once if the route defines no expectation is set for the route). Unlike the other methods for inspecting calls, unmatched calls are irrelevant. Therefore, if no filter is passed, done() returns true if every route has been called the number of expected times.

calls(filter, method)

Returns an object {matched: [], unmatched: []} containing arrays of all calls to fetch, grouped by whether fetch-mock matched them or not. If filter is specified then only calls to fetch matching that route are returned.

lastCall(filter, method)

Returns the arguments for the last matched call to fetch

lastUrl(filter, method)

Returns the url for the last matched call to fetch. When fetch was last called using a Request instance, the url will be extracted from this

lastOptions(filter, method)

Returns the options for the last matched call to fetch. When fetch was last called using a Request instance, the entire Request instance will be returned


Returns a Promise that resolves once all fetches handled by fetch-mock have resolved. Useful for testing code that uses fetch but doesn’t return a promise.


On either the global or sandboxed fetchMock instances, the following config options can be set by setting properties on fetchMock.config. Many can also be set on individual calls to .mock().

Note that Object.assign(fetchMock.config, require('fetch-ponyfill')()) will configure fetch-mock to use all of fetch-ponyfill’s classes. In most cases, it should only be necessary to set this once before any tests run.