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 everythingmockResponseaccepts (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
mockResponseOnceoutranks routes for its call; a matching route outranks the ambient default and the conditional gate — so routes fire even underdontMock(), 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.
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).