npm jest-fetch-mock 4.0.0-beta.1

one hour ago

The modernization beta — please test: npm install -D jest-fetch-mock@next

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

Added

  • createFetchMock(jest) factory + dependency-free jest-fetch-mock/factory entry (injectGlobals: false / @jest/globals support)
  • "setupFilesAfterEnv": ["jest-fetch-mock/setup"] one-liner
  • resetMocks: true auto-re-arm — the years-old "fetch returns undefined" footgun (#78, #81, #202) is fixed when setup runs in setupFilesAfterEnv
  • AbortSignal.timeout() rejects with its TimeoutError; custom abort reasons pass through (#242)
  • fetchMock.defaultResponseInit — default headers/init 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 (3.2.0 remains for older stacks)
  • disableMocks() restores the environment's original fetch
  • TypeScript definitions are self-contained: no @types/jest requirement (#248), no forced dom lib (#201) — you need ambient fetch types from lib: ["dom"] or @types/node ≥ 18, and under node types response.json() is honestly Promise<unknown>
  • Relative URLs in node environments resolve against http://localhost/ for matching
  • domexception dependency removed

Unchanged

Every 3.x API, alias, chaining behavior, once-queue semantic, and abort message. The full 3.x suite and all seven consumer integration fixtures pass against this beta unmodified.

Migration guide: https://github.com/jefflau/jest-fetch-mock#migrating-from-3x — feedback on #263, please.

Don't miss a new jest-fetch-mock release

NewReleases is sending notifications on new releases.