Skip to content

4.0.0 — native fetch, no more global stomping

Choose a tag to compare

@jefflau jefflau released this 08 Jul 08:41

jest-fetch-mock 4.0 no longer replaces the global fetch classes with node-fetch implementations. In environments that provide fetch primitives (jest-environment-node, Jest 28+), mocked Responses are real native (undici) responses — instanceof works, streams and FormData behave, and nothing your tests didn't ask for is touched. Where the environment has none (jest-environment-jsdom), the cross-fetch fallback fills exactly the missing globals, as it always did.

Every 3.x API is unchanged. The 3.x unit suite and seven consumer integration fixtures (React + Testing Library, TypeScript strict under three tsconfigs, jsdom, Jest 30, native-fetch hosts, a real-HTTP-server passthrough e2e) pass against 4.0 unmodified.

Added

  • createFetchMock(jest) factory + dependency-free jest-fetch-mock/factory entry — injectGlobals: false / @jest/globals setups finally work (#248)
  • "setupFilesAfterEnv": ["jest-fetch-mock/setup"] one-liner
  • resetMocks: true auto-re-arm — the years-old "fetch returns undefined" footgun (#78, #81, #202) is gone when setup runs in setupFilesAfterEnv
  • AbortSignal.timeout() rejects with its TimeoutError; custom abort reasons pass through (#242)
  • fetchMock.defaultResponseInit — default init (e.g. JSON headers) merged under every mock (#166)
  • fetchMock.realFetch (reassignable passthrough target) and fetchMock.usingNativeFetch
  • ESM wrappers + exports map with a ./* escape hatch

Changed (breaking)

  • Floors: Node ≥ 18, Jest ≥ 28 — stay on 3.2.0 for older stacks
  • disableMocks() restores the environment's original fetch
  • Self-contained TypeScript definitions: no @types/jest requirement, no forced dom lib — ambient fetch types come from lib: ["dom"] or @types/node ≥ 18; under node types response.json() is honestly Promise<unknown>
  • Relative URLs in node environments resolve against http://localhost/ for match predicates
  • domexception dependency removed

Migration guide: https://github.com/jefflau/jest-fetch-mock#migrating-from-3xCHANGELOG