{"id":48215,"library":"jest-fetch-mock-fork","title":"Jest Fetch Mock (Fork)","description":"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.","status":"active","version":"3.1.2","language":"javascript","source_language":"en","source_url":"https://github.com/lukaselmer/jest-fetch-mock","tags":["javascript","jest","vitest","mock","fetch","typescript"],"install":[{"cmd":"npm install jest-fetch-mock-fork","lang":"bash","label":"npm"},{"cmd":"yarn add jest-fetch-mock-fork","lang":"bash","label":"yarn"},{"cmd":"pnpm add jest-fetch-mock-fork","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Polyfills fetch in Node.js for consistent mocking behavior.","package":"cross-fetch","optional":true}],"imports":[{"note":"CommonJS is the default module system for Jest; ESM import may require 'esModuleInterop' or similar config.","wrong":"import { enableMocks } from 'jest-fetch-mock-fork'","symbol":"enableMocks","correct":"const { enableMocks } = require('jest-fetch-mock-fork')"},{"note":"fetchMock is a named export, not default. Also available globally after enableMocks() is called.","wrong":"import fetchMock from 'jest-fetch-mock-fork'","symbol":"fetchMock","correct":"const { fetchMock } = require('jest-fetch-mock-fork')"},{"note":"The default export is the object containing enableMocks, fetchMock, etc. Use destructuring for clarity.","wrong":"import GlobalFetch from 'jest-fetch-mock-fork'","symbol":"default (as GlobalFetch)","correct":"const GlobalFetch = require('jest-fetch-mock-fork')"}],"quickstart":{"code":"// setupJest.js\nrequire('jest-fetch-mock-fork').enableMocks()\n\n// __tests__/api.test.js\nconst { fetchMock } = require('jest-fetch-mock-fork')\n\nbeforeEach(() => {\n  fetchMock.resetMocks()\n})\n\ntest('fetches user data', async () => {\n  fetchMock.mockResponseOnce(JSON.stringify({ name: 'John' }))\n\n  const response = await fetch('/api/user/1')\n  const data = await response.json()\n\n  expect(data.name).toEqual('John')\n  expect(fetchMock).toHaveBeenCalledWith('/api/user/1')\n})\n\ntest('handles network error', async () => {\n  fetchMock.mockRejectOnce(new Error('Network failure'))\n\n  await expect(fetch('/nonexistent')).rejects.toThrow('Network failure')\n  expect(fetchMock).toHaveBeenCalledTimes(1)\n})","lang":"javascript","description":"Demonstrates basic setup with enableMocks, then a test for a successful fetch and one for a rejected fetch using mockResponseOnce and mockRejectOnce."},"warnings":[{"fix":"Set 'resetMocks: false' in Jest config if using setupJest.js with enableMocks(), or move enableMocks() to a beforeEach block.","message":"Jest 4.0.1 changed default for resetMocks from false to true, which can cause unexpected mock resets.","severity":"gotcha","affected_versions":">=4.0.1"},{"fix":"Update test code to use the new API: mockResponseOnce(body, init) where init is an object with status, headers, etc.","message":"Upgrading from original jest-fetch-mock (<1.0) may have breaking changes in API (e.g., mockResponseOnce parameters).","severity":"breaking","affected_versions":"<1.0"},{"fix":"Always pass body and init as pairs. Use fetchMock.mockResponses([body1, init1], [body2, init2], ...) or pass multiple pairs.","message":"fetchMock.mockResponses expects an even number of arguments (body, init pairs); wrong count causes silent failures.","severity":"gotcha","affected_versions":"*"},{"fix":"Use fetchMock.mockIf(/pattern/, handler) to scope mocks to specific URLs, or call fetchMock.doMock() to enable global mocking explicitly.","message":"Using fetchMock.mock(() => ...) without specifying URL pattern may mock all fetch calls globally.","severity":"deprecated","affected_versions":"*"}],"env_vars":null,"search_vec":"'3.1.2':19 'altern':63 'api':77 'browser':58 'browser-lik':57 'case':81 'common':79 'compar':61 'compat':52 'cross':45 'cross-fetch':44 'current':21 'differ':37 'direct':68 'environ':17 'error':41 'fetch':2,10,46,92,109 'fetchmock':26 'fix':104 'fork':4,86 'global':25 'http':38 'integr':67 'javascript':105 'jest':1,13,70,91,106 'jest-fetch-mock':90 'latest':103 'librari':7 'lightweight':6 'like':30,59,64 'method':29 'mock':3,9,72,93,108 'mockreject':34 'mockrespons':32 'mockresponseonc':31 'nock':65 'node.js':55 'note':82 'npm':101 'object':27 'offer':74 'origin':89 'polyfil':47 'provid':23 'publish':99 'request':11 'respons':39 'runtim':60 'simpler':76 'simul':36 'support':48 'system':73 'test':16 'typescript':49,110 'unmaintain':96 'use':80 'version':18 'vitest':15,107 'work':42","created_at":"2026-06-07T16:55:29.141869+00:00","updated_at":"2026-06-07T16:55:29.141869+00:00","problems":[{"fix":"Ensure enableMocks() is called in setup file before tests, and use const { fetchMock } = require('jest-fetch-mock-fork') if needed.","cause":"fetchMock not properly initialized; maybe enableMocks() not called or fetchMock imported incorrectly.","error":"TypeError: fetchMock.mockResponseOnce is not a function"},{"fix":"Add \"resetMocks\": false to Jest configuration in package.json, or move enableMocks() to a beforeEach() in each test file.","cause":"Jest version >=4.0.1 has changed default resetMocks to true, conflicting with setup file pattern.","error":"Jest: \"resetMocks\" is set to true by default. This will cause mocks to be reset automatically."},{"fix":"Use the global fetchMock variable (after enableMocks()) or import/require as CommonJS: const { fetchMock } = require('jest-fetch-mock-fork')","cause":"fetchMock is not the same instance as the one used internally (e.g., using import instead of require).","error":"fetchMock.mock.calls is undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/lukaselmer/jest-fetch-mock#readme","github":"https://github.com/lukaselmer/jest-fetch-mock","docs":null,"changelog":null,"pypi":null,"npm":"jest-fetch-mock-fork","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}