npm jest-fetch-mock 4.2.0
4.2.0 — route-based mocking

3 hours ago

The most-requested feature finally lands (#171, filed in 2020): registered routes that coexist.

fetchMock
  .route('https://api.example.com/session', JSON.stringify({ user: 'jeff' }))
  .route(/\/teams$/, JSON.stringify([{ id: 42 }]))
  .routeOnce('https://api.example.com/login', '', { status: 401 })
  • Matchers are the familiar shapes (URL string, RegExp, (request) => boolean); responses are everything mockResponse accepts (string, Response, sync/async function). First registered match wins.
  • routeOnce() is consumed by its first match; clearRoutes() empties the registry; resetMocks() clears routes too.
  • Precedence: a queued mockResponseOnce outranks routes for its call; a matching route outranks the ambient default and the conditional gate — so routes fire even under dontMock(), making "mock these endpoints, let everything else hit the network" a one-liner each.
  • mockReject()/mockAbort() override routes. Existing behavior is untouched — every pre-4.2 test passes unmodified.

Docs: RoutesCHANGELOG

Also in this release: the publish workflow no longer self-upgrades npm on the runner (it corrupted itself mid-run once; Node 24's bundled npm already supports trusted publishing).

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

NewReleases is sending notifications on new releases.