Registry / testing / jest-fetch-mock-fork

jest-fetch-mock-fork

JSON →
library3.1.2jsnpmunverified

A lightweight library for mocking fetch requests in Jest and Vitest test environments. Version 3.1.2 is current. It provides a global fetchMock object with methods like mockResponseOnce, mockResponses, and mockReject to simulate different HTTP responses and errors. Works with cross-fetch polyfill, supports TypeScript, and is compatible with both Node.js and browser-like runtimes. Compared to alternatives like nock, it integrates directly with Jest's mocking system, offering a simpler API for common use cases. Note: This is a fork of the original jest-fetch-mock, which is unmaintained and not published to npm with latest fixes.

npm install jest-fetch-mock-fork
INSTALL
IMPORT
SIG · JEST-FETCH-MOCK-FO
J
jest-fetch-mock-fork
testingjavascriptv3.1.2
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
const { enableMocks } = require('jest-fetch-mock-fork')
import { enableMocks } from 'jest-fetch-mock-fork'
CommonJS is the default module system for Jest; ESM import may require 'esModuleInterop' or similar config.
fetchMock
const { fetchMock } = require('jest-fetch-mock-fork')
import fetchMock from 'jest-fetch-mock-fork'
fetchMock is a named export, not default. Also available globally after enableMocks() is called.
default (as GlobalFetch)
const GlobalFetch = require('jest-fetch-mock-fork')
import GlobalFetch from 'jest-fetch-mock-fork'
The default export is the object containing enableMocks, fetchMock, etc. Use destructuring for clarity.

Demonstrates basic setup with enableMocks, then a test for a successful fetch and one for a rejected fetch using mockResponseOnce and mockRejectOnce.

// setupJest.js require('jest-fetch-mock-fork').enableMocks() // __tests__/api.test.js const { fetchMock } = require('jest-fetch-mock-fork') beforeEach(() => { fetchMock.resetMocks() }) test('fetches user data', async () => { fetchMock.mockResponseOnce(JSON.stringify({ name: 'John' })) const response = await fetch('/api/user/1') const data = await response.json() expect(data.name).toEqual('John') expect(fetchMock).toHaveBeenCalledWith('/api/user/1') }) test('handles network error', async () => { fetchMock.mockRejectOnce(new Error('Network failure')) await expect(fetch('/nonexistent')).rejects.toThrow('Network failure') expect(fetchMock).toHaveBeenCalledTimes(1) })
Debug
Known issues
gotchaJest 4.0.1 changed default for resetMocks from false to true, which can cause unexpected mock resets.
fix
Set 'resetMocks: false' in Jest config if using setupJest.js with enableMocks(), or move enableMocks() to a beforeEach block.
affects: >=4.0.1
breakingUpgrading from original jest-fetch-mock (<1.0) may have breaking changes in API (e.g., mockResponseOnce parameters).
fix
Update test code to use the new API: mockResponseOnce(body, init) where init is an object with status, headers, etc.
affects: <1.0
gotchafetchMock.mockResponses expects an even number of arguments (body, init pairs); wrong count causes silent failures.
fix
Always pass body and init as pairs. Use fetchMock.mockResponses([body1, init1], [body2, init2], ...) or pass multiple pairs.
affects: *
deprecatedUsing fetchMock.mock(() => ...) without specifying URL pattern may mock all fetch calls globally.
fix
Use fetchMock.mockIf(/pattern/, handler) to scope mocks to specific URLs, or call fetchMock.doMock() to enable global mocking explicitly.
affects: *
Errors
Common errors & fixes
TypeError: fetchMock.mockResponseOnce is not a function
fetchMock not properly initialized; maybe enableMocks() not called or fetchMock imported incorrectly.
fix
Ensure enableMocks() is called in setup file before tests, and use const { fetchMock } = require('jest-fetch-mock-fork') if needed.
Jest: "resetMocks" is set to true by default. This will cause mocks to be reset automatically.
Jest version >=4.0.1 has changed default resetMocks to true, conflicting with setup file pattern.
fix
Add "resetMocks": false to Jest configuration in package.json, or move enableMocks() to a beforeEach() in each test file.
fetchMock.mock.calls is undefined
fetchMock is not the same instance as the one used internally (e.g., using import instead of require).
fix
Use the global fetchMock variable (after enableMocks()) or import/require as CommonJS: const { fetchMock } = require('jest-fetch-mock-fork')
Upgrade
Version history
3.1.2latest on npm
Audit
Dependencies
cross-fetchoptionalPolyfills fetch in Node.js for consistent mocking behavior.
Agent activity
20 hits · last 30 days
node
18
Resources
jest-fetch-mock-fork — npm install jest-fetch-mock-fork · libregistry