A test framework integrated fetch mocking solution written as a native ESModule, compatible with Jest, Vitest, and other test frameworks that support a similar API (getMockImplementation, mockImplementation, mock.calls, mock.results, mock.lastCall). Version 0.0.33 is the latest stable release, with ongoing development. Unlike other fetch mocking libraries, fetch-mocked is a pure ESModule with no CommonJS dependencies, making it fully compatible with native ESModule projects. It provides an enriched mock function that integrates assertions and mock utilities from the test framework, along with polyfills for fetch, Request, Response, and Headers using node-fetch.
npm install fetch-mockedNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup and usage of mockFetch with Vitest, including lifecycle hooks and a test that mocks a fetch call and verifies behavior.
Pass the factory function itself: mockFetch(vi.fn) not mockFetch(vi.fn()).
Ensure setupFiles (vitest) or setupFilesAfterEnv (jest) includes './node_modules/fetch-mocked/testSetup.mjs'.
Set responseType globally only if consistent across all mocks; otherwise, pass it in individual mock options.
If using the setup file, do not manually call polyfillFetch. If already polyfilled, consider disabling the setup file.
Run 'npm install fetch-mocked -D' and verify the path in your jest config matches the installation path.
Use mockFetch(jest.fn) (no parentheses) to pass the factory function.
Add the setup file or import and call polyfillFetch() at the top of your test file.
Import MockedFetch type and cast if needed: const mockedFetch = mockFetch(vi.fn) as MockedFetch;