A lightweight library (v1.0.7, no recent releases) for mocking HTTP requests and responses using the fetch API or isomorphic-fetch in unit tests. It provides a simple chainable API: when().withExpectedHeader().otherwiseRespondWith().respondWith(), plus failNextCall() for network errors. Compared to fetch-mock, it is less active and feature-limited but easier to read.
npm install mock-fetch-apiNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Quick example of mocking a GET request with mock-fetch-api in a test.
Set global.fetch = MockFetch.fetch before your tests.
Consistently use two strings or one object, but mixing may cause unexpected behavior.
Pass a JSON string, not a JavaScript object, as the second argument.
Re-apply failNextCall before each test that expects a network error.
Follow the documented chain: when().withExpectedHeader().otherwiseRespondWith().respondWith().
N/A
Add global.fetch = MockFetch.fetch; before using fetch.
Use import MockFetch from 'mock-fetch-api' or const MockFetch = require('mock-fetch-api').Use const MockFetch = require('mock-fetch-api') without destructuring.Ensure when() matches the exact method and URL in your fetch call.
Wrap your data in JSON.stringify() or pass a string like '{"key":"value"}'.