Overview
fetch-mock is the most comprehensive library for mocking the fetch API.
It allows mocking http requests made using fetch or a library imitating its api, such as node-fetch.
It supports most JavaScript environments, including browsers, Node.js, web workers and service workers.
As well as shorthand methods for the simplest use cases, it offers a flexible API for customising all aspects of mocking behaviour.
For documentation for fetch-mock v11 and below visit the Legacy API docs
Examples
import fetchMock from 'fetch-mock';
fetchMock.mockGlobal().route('http://example.com', 200);
const res = await fetch('http://example.com');
assert(res.ok);
import fetchMock from 'fetch-mock';
fetchMock
.mockGlobal()
.route({
express: '/api/users/:user'
expressParams: {user: 'kenneth'}
}, {
userData: {
email: '[email protected]'
}
}, 'userDataFetch');
const res = await fetch('http://example.com/api/users/kenneth');
assert(fetchMock.called('userDataFetch'))
const data = await res.json();
assertEqual(data.userData.email, '[email protected]')