Registry / testing / meiro-jest-fetch-mock

meiro-jest-fetch-mock

JSON →
library3.0.4jsnpmunverified

Mock for fetch calls in Jest tests. v3.0.4 enables mocking global fetch (including cross-fetch polyfill) with Jest's built-in mock functions. It provides a simple API with mockResponse, mockResponses, mockReject, and mockOnce methods. Supports TypeScript types, conditional mocking by URL, and resetting state between tests. Lightweight alternative to MSW or nock for unit tests.

npm install meiro-jest-fetch-mock
INSTALL
IMPORT
SIG · MEIRO-JEST-FETCH-M
M
meiro-jest-fetch-mock
testingjavascriptv3.0.4
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.

enableMocks
import { enableMocks } from 'jest-fetch-mock'
import jestFetchMock from 'jest-fetch-mock'
enableMocks is a named export. Default import is not available.
fetchMock
// After enableMocks(), fetchMock is available globally fetchMock.mockResponse('ok')
import { fetchMock } from 'jest-fetch-mock'
fetchMock is not a named export; it becomes a global after calling enableMocks().
GlobalFetchMock
import type { GlobalFetchMock } from 'jest-fetch-mock'
import { GlobalFetchMock } from 'jest-fetch-mock'
GlobalFetchMock is a TypeScript type export, named import for type-only usage.

Setup with enableMocks() and basic mock/assert pattern using fetchMock.mockResponse and Jest matchers.

// setupJest.js (or similar setup file) require('jest-fetch-mock').enableMocks(); // yourComponent.test.js test('fetches data', async () => { fetchMock.mockResponse(JSON.stringify({ data: 'test' })); const response = await fetch('/api/data'); const json = await response.json(); expect(json.data).toBe('test'); expect(fetch).toHaveBeenCalledTimes(1); expect(fetch).toHaveBeenCalledWith('/api/data'); });
Debug
Known issues
breakingJest's resetMocks default changed to true in Jest 4.0.1 causing fetchMock to reset unexpectedly
fix
Set Jest config 'resetMocks: false' if using setup file to call enableMocks(), or call enableMocks() in beforeEach.
affects: >=3.0.0
gotchafetch is replaced globally by enableMocks — using real fetch after mock requires donMock()
fix
Call fetchMock.dontMock() after enableMocks() to default to real fetch, then doMock() per test.
affects: >=2.0.0
deprecatedmockResponseOnce and mockRejectOnce are deprecated in favor of mockOnce
fix
Use fetchMock.once().mockResponse(...) or fetchMock.once().mockReject(...) chain.
affects: >=3.0.0
gotchafetchMock is not available as an import — must call enableMocks() first
fix
Always call enableMocks() in setup file; then use global fetchMock variable.
affects: >=3.0.0
gotchamockResponses does not clear previous mock — can accumulate
fix
Call fetchMock.resetMocks() before using mockResponses in a test.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: fetchMock.mockResponse is not a function
enableMocks() was not called, so fetchMock global is undefined or not fully initialized.
fix
Ensure setupJest.js calls require('jest-fetch-mock').enableMocks() and Jest setupFiles config points to it.
Cannot find module 'jest-fetch-mock' from 'setupJest.js'
jest-fetch-mock is not installed or node_modules missing.
fix
Run npm install --save-dev jest-fetch-mock and ensure it's in devDependencies.
jest.fn() is not a function
Incorrect import or setup; jest not available in setup file.
fix
Ensure Jest is configured properly; setup file should be in setupFiles not setupFilesAfterFramework.
Upgrade
Version history
3.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
meiro-jest-fetch-mock — npm install meiro-jest-fetch-mock · libregistry