1 | # fetch-mock
|
2 |
|
3 | Mock http requests made using [fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch).
|
4 |
|
5 | Features include:
|
6 |
|
7 | - mocks most of the fetch API spec, even advanced behaviours such as streaming and aborting
|
8 | - declarative matching for most aspects of a http request, including url, headers, body and query parameters
|
9 | - shorthands for the most commonly used features, such as matching a http method or matching one fetch only
|
10 | - support for delaying responses, or using your own async functions to define custom race conditions
|
11 | - can be used as a spy to observe real network requests
|
12 | - can be extended with your own reusable custom matchers that can be used both for matching fetch-calls and inspecting the results
|
13 | - isomorphic, and supports either a global fetch instance or a locally required instance
|
14 |
|
15 | _New_ If using jest, try the new [fetch-mock-jest](https://www.npmjs.com/package/fetch-mock-jest) wrapper.
|
16 |
|
17 | _New_ [Cheatsheet](https://github.com/wheresrhys/fetch-mock/blob/master/docs/cheatsheet.md)
|
18 |
|
19 | ![node version](https://img.shields.io/node/v/fetch-mock.svg?style=flat-square)
|
20 | [![licence](https://img.shields.io/npm/l/fetch-mock.svg?style=flat-square)](https://github.com/wheresrhys/fetch-mock/blob/master/LICENSE)
|
21 | ![npm downloads](https://img.shields.io/npm/dm/fetch-mock.svg?style=flat-square)
|
22 | [![CircleCI](https://img.shields.io/circleci/project/github/wheresrhys/fetch-mock.svg?style=flat-square)](https://circleci.com/gh/wheresrhys/workflows/fetch-mock)
|
23 | [![Code coverage](https://img.shields.io/coveralls/github/wheresrhys/fetch-mock.svg?style=flat-square)](https://coveralls.io/github/wheresrhys/fetch-mock)
|
24 | [![Known Vulnerabilities](https://snyk.io/test/github/wheresrhys/fetch-mock/badge.svg?targetFile=package.json&style=flat-square)](https://snyk.io/test/github/wheresrhys/fetch-mock?targetFile=package.json)
|
25 |
|
26 | ```js
|
27 | fetchMock.mock('http://example.com', 200);
|
28 | const res = await fetch('http://example.com');
|
29 | assert(res.ok);
|
30 | fetchMock.restore();
|
31 | ```
|
32 |
|
33 | ## Table of Contents
|
34 |
|
35 | - [Requirements](#requirements)
|
36 | - [Documentation and Usage](http://www.wheresrhys.co.uk/fetch-mock/)
|
37 | - [License](#license)
|
38 | - [Housekeeping](#housekeeping)
|
39 |
|
40 | **I devote a lot of time to maintaining fetch-mock for free. I don't ask for payment, but am raising money for a refugee charity - <a href="https://www.justgiving.com/refugee-support-europe">please consider donating</a>**
|
41 |
|
42 | ---
|
43 |
|
44 | ## Requirements
|
45 |
|
46 | fetch-mock requires the following to run:
|
47 |
|
48 | - [Node.js](https://nodejs.org/) 8+ for full feature operation
|
49 | - [Node.js](https://nodejs.org/) 0.12+ with [limitations](http://www.wheresrhys.co.uk/fetch-mock/#usageinstallation)
|
50 | - [npm](https://www.npmjs.com/package/npm) (normally comes with Node.js)
|
51 | - Either of the following
|
52 | - [node-fetch](https://www.npmjs.com/package/node-fetch) when testing in a nodejs
|
53 | - A browser that supports the `fetch` API when testing in a browser
|
54 |
|
55 | ## Documentation and Usage
|
56 |
|
57 | See the [project website](http://www.wheresrhys.co.uk/fetch-mock/) or [cheatsheet](https://github.com/wheresrhys/fetch-mock/blob/master/docs/cheatsheet.md)
|
58 |
|
59 | If you're using jest as your test runner, consider using [fetch-mock-jest](https://www.npmjs.com/package/fetch-mock-jest), a lightweight, jest-friendly wrapper around fetch-mock.
|
60 |
|
61 | ## License
|
62 |
|
63 | fetch-mock is licensed under the [MIT](https://github.com/wheresrhys/fetch-mock/blob/master/LICENSE) license.
|
64 | Copyright © 2019, Rhys Evans
|
65 |
|
66 | ## Housekeeping
|
67 |
|
68 | ![npm version](https://img.shields.io/npm/v/fetch-mock.svg?style=flat-square)
|
69 | [![maintainability](https://api.codeclimate.com/v1/badges/7f8abbf54ec9f3d07df3/maintainability?style=flat-square)](https://codeclimate.com/github/wheresrhys/fetch-mock/maintainability)
|