Installation
Install fetch-mock using
npm install --save-dev fetch-mock
fetch-mock supports both ES modules and commonjs. The following should work in most environments.
ES modules
import fetchMock from 'fetch-mock';
Commonjs
const fetchMock = require('fetch-mock');
Using alongside other testing frameworks
When using one of the following frameworks consider using the appropriate wrapper library, which modify/extend fetch-mock to make using it more idiomatic to your testing environment e.g. adding methods equivalent to Jest's mockRestore()
etc.
- Jest - @fetch-mock/jest
- Vitest - @fetch-mock/vitest
Deno support
Import fetch-mock, or the wrappers above, using the npm:
prefix, e.g.
import fetchMock from 'npm:[email protected]';