Registry / testing / mock-fetch-api

mock-fetch-api

JSON →
library1.0.7jsnpmunverified

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-api
INSTALL
IMPORT
SIG · MOCK-FETCH-API
M
mock-fetch-api
testingjavascriptv1.0.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MockFetch
import MockFetch from 'mock-fetch-api'
import { MockFetch } from 'mock-fetch-api'
Default export only; named import will result in undefined.
MockFetch (require)
const MockFetch = require('mock-fetch-api')
const { MockFetch } = require('mock-fetch-api')
CommonJS require works fine; destructuring fails.
fetch
global.fetch = MockFetch.fetch
fetch = MockFetch
You must assign MockFetch.fetch to the global fetch function for it to intercept calls.

Quick example of mocking a GET request with mock-fetch-api in a test.

import MockFetch from 'mock-fetch-api'; global.fetch = MockFetch.fetch; MockFetch.when('GET', 'http://example.com/api') .respondWith(200, '{"message":"ok"}'); const response = await fetch('http://example.com/api', { method: 'GET' }); const data = await response.json(); console.log(data.message); // 'ok'
Debug
Known issues
gotchaMust assign MockFetch.fetch to global.fetch for interception to work
fix
Set global.fetch = MockFetch.fetch before your tests.
affects: >=1.0.0
gotchawithExpectedHeader can accept either two arguments (string, string) or one object argument
fix
Consistently use two strings or one object, but mixing may cause unexpected behavior.
affects: >=1.0.0
gotcharespondWith takes status and data (as string, not object)
fix
Pass a JSON string, not a JavaScript object, as the second argument.
affects: >=1.0.0
gotchaCalling failNextCall() makes the next fetch reject, but only once; subsequent calls succeed normally
fix
Re-apply failNextCall before each test that expects a network error.
affects: >=1.0.0
gotchaMethods are chainable but order matters: withExpectedHeader must come before otherwiseRespondWith and respondWith
fix
Follow the documented chain: when().withExpectedHeader().otherwiseRespondWith().respondWith().
affects: >=1.0.0
breakingNo breaking changes recorded; patch-level updates only
fix
N/A
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetch is not a function
MockFetch.fetch not assigned to global.fetch
fix
Add global.fetch = MockFetch.fetch; before using fetch.
TypeError: Cannot read properties of undefined (reading 'then')
Using named import { MockFetch } instead of default import
fix
Use import MockFetch from 'mock-fetch-api' or const MockFetch = require('mock-fetch-api').
TypeError: MockFetch.when is not a function
Destructuring require incorrectly
fix
Use const MockFetch = require('mock-fetch-api') without destructuring.
AssertionError: expected 404 to equal 200
URL or method mismatch; mock not set up for that request
fix
Ensure when() matches the exact method and URL in your fetch call.
TypeError: response.json is not a function
Response body is not JSON; respondWith data must be a JSON string
fix
Wrap your data in JSON.stringify() or pass a string like '{"key":"value"}'.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies
isomorphic-fetchoptionaloptional peer - used to polyfill fetch in Node
Agent activity
4 hits · last 30 days
node
4
Resources