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-fetchNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows configuring the mock in Jest setup, using fetch.mockResponse to simulate a response, and asserting on fetch calls and async behavior.
Update calls to fetch in tests to use (resource, init) where init is optional. Old code: fetch('/url', null, config) => new: fetch('/url', config).Change fetch.mockResponse('string') to fetch.mockResponse({ text: () => 'string' }).Always call fetch.reset() in afterEach to clear internal state.
If using a polyfill, set up manual mock as described in README to avoid conflicts.
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.Change fetch.mockResponse('string') to fetch.mockResponse({ text: () => 'string' }).Verify that global.fetch = fetch is set in setup. In tests, use async/await to wait for promise resolution.