Registry / testing / jest-mock-fetch

jest-mock-fetch

JSON →
library2.0.5jsnpmunverified

An ultra-lightweight synchronous fetch mock for Jest, designed to mock the native fetch API or polyfill libraries like unfetch. Version 2.0.5 is stable and actively maintained. Key differentiators: synchronous promise resolution for deterministic tests, simple API (mockResponse, mockError, lastReqGet, lastPromiseGet), and TypeScript support. It provides a mock fetch function that can be used as a Jest spy, enabling assertions on calls and simulation of server responses. Breaking changes in v2.0.0 aligned the API with the official fetch spec.

npm install jest-mock-fetch
INSTALL
IMPORT
SIG · JEST-MOCK-FETCH
J
jest-mock-fetch
testingjavascriptv2.0.5
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.

fetch
import fetch from 'jest-mock-fetch';
const fetch = require('jest-mock-fetch');
Default import is the main way to get the mock fetch function. In CJS, you can use require but must call fetch.default or use require('jest-mock-fetch').default in some setups.
fetch.mockResponse
fetch.mockResponse({ text: () => 'OK' });
fetch.mockResponse('OK');
In v2.0.0+, mockResponse accepts an object with response methods (text, json, etc.) instead of a simple string. The old signature with data param no longer works.
fetch.reset
fetch.reset();
fetch.mockClear();
The mock provides a custom reset method to clear internal state, including resolved promises. Using Jest's mockClear is insufficient for complete cleanup.

Shows configuring the mock in Jest setup, using fetch.mockResponse to simulate a response, and asserting on fetch calls and async behavior.

// setupJest.js const fetch = require('jest-mock-fetch'); global.fetch = fetch; // test.spec.js import myFetchFn from './myFetchFn'; beforeEach(() => { fetch.reset(); }); it('should call fetch with correct URL', async () => { const responseText = 'mock response'; fetch.mockResponse({ text: () => responseText }); const result = await myFetchFn('https://api.example.com/data'); expect(fetch).toHaveBeenCalledWith('https://api.example.com/data'); expect(result).toBe(responseText); });
Debug
Known issues
breakingv2.0.0 changed the fetch function signature from 3 arguments (url, data, config) to 2 arguments (resource, init).
fix
Update calls to fetch in tests to use (resource, init) where init is optional. Old code: fetch('/url', null, config) => new: fetch('/url', config).
affects: >=2.0.0
breakingv2.0.0: mockResult no longer accepts a data parameter; you must provide implementation for response methods like text, json, etc.
fix
Change fetch.mockResponse('string') to fetch.mockResponse({ text: () => 'string' }).
affects: >=2.0.0
gotchaThe mock is synchronous; it resolves promises immediately in test code, but if you forget to call reset() after each test, responses may leak between tests.
fix
Always call fetch.reset() in afterEach to clear internal state.
affects: >=1.0.0
gotchaGlobal fetch mock may interfere with other libraries that depend on the native fetch. Use with caution in test environments.
fix
If using a polyfill, set up manual mock as described in README to avoid conflicts.
affects: >=1.0.0
Errors
Common errors & fixes
fetch.mockResponse is not a function
Importing the module incorrectly (e.g., using require without .default) or not calling fetch.reset before use.
fix
Ensure you import fetch correctly: import fetch from 'jest-mock-fetch' (ESM) or const fetch = require('jest-mock-fetch').default (CJS). Also, call fetch.reset() in beforeEach.
TypeError: Cannot read properties of undefined (reading 'text')
Calling fetch.mockResponse with a string instead of an object in v2.0.0+.
fix
Change fetch.mockResponse('string') to fetch.mockResponse({ text: () => 'string' }).
Expected mock function to have been called with: ... but it was never called.
The mock may not be set as global.fetch, or the async test did not await the promise.
fix
Verify that global.fetch = fetch is set in setup. In tests, use async/await to wait for promise resolution.
Upgrade
Version history
2.0.5latest on npm
Audit
Dependencies
jestrequiredPeer dependency: the mock is designed to integrate with Jest's test framework and spy mechanism.
Agent activity
11 hits · last 30 days
node
10
Resources
jest-mock-fetch — npm install jest-mock-fetch · libregistry