You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+25-24
Original file line number
Diff line number
Diff line change
@@ -2,22 +2,22 @@
2
2
3
3
Fetch is the new way to do HTTP requests in the browser, and it can be used in other environments such as React Native. Jest Fetch Mock allows you to easily mock your `fetch` calls and return the response you need to fake the HTTP requests. It's easy to setup and you don't need a library like `nock` to get going and it uses Jest's built-in support for mocking under the surface. This means that any of the `jest.fn()` methods are also available. For more information on the jest mock API, check their docs [here](https://facebook.github.io/jest/docs/en/mock-functions.html)
4
4
5
-
It currently supports the mocking of the go-to isomorphic polyfill for fetch, [`isomorphic-fetch`](https://github.com/matthew-andrews/isomorphic-fetch), so it supports Node.js and any browser-like runtime.
5
+
It currently supports the mocking with the [`cross-fetch`](https://www.npmjs.com/package/cross-fetch) polyfill, so it supports Node.js and any browser-like runtime.
6
6
7
7
## Contents
8
8
9
-
*[Usage](#usage)
10
-
*[Installation and Setup](#installation-and-setup)
11
-
*[Using with Create-React-App](#using-with-create-react-app)
12
-
*[API](#api)
13
-
*[Examples](#examples)
14
-
*[Simple mock and assert](#simple-mock-and-assert)
15
-
*[Mocking all fetches](#mocking-all-fetches)
16
-
*[Mocking a failed fetch](#mocking-a-failed-fetch)
17
-
*[Mocking multiple fetches with different responses](#mocking-multiple-fetches-with-different-responses)
18
-
*[Mocking multiple fetches with `fetch.mockResponses`](#mocking-multiple-fetches-with-fetchmockresponses)
19
-
*[Reset mocks between tests with `fetch.resetMocks`](#reset-mocks-between-tests-with-fetchresetmocks)
20
-
*[Using `fetch.mock` to inspect the mock state of each fetch call](#using-fetchmock-to-inspect-the-mock-state-of-each-fetch-call)
9
+
-[Usage](#usage)
10
+
-[Installation and Setup](#installation-and-setup)
11
+
-[Using with Create-React-App](#using-with-create-react-app)
12
+
-[API](#api)
13
+
-[Examples](#examples)
14
+
-[Simple mock and assert](#simple-mock-and-assert)
15
+
-[Mocking all fetches](#mocking-all-fetches)
16
+
-[Mocking a failed fetch](#mocking-a-failed-fetch)
17
+
-[Mocking multiple fetches with different responses](#mocking-multiple-fetches-with-different-responses)
18
+
-[Mocking multiple fetches with `fetch.mockResponses`](#mocking-multiple-fetches-with-fetchmockresponses)
19
+
-[Reset mocks between tests with `fetch.resetMocks`](#reset-mocks-between-tests-with-fetchresetmocks)
20
+
-[Using `fetch.mock` to inspect the mock state of each fetch call](#using-fetchmock-to-inspect-the-mock-state-of-each-fetch-call)
21
21
22
22
## Usage
23
23
@@ -51,6 +51,8 @@ Add the setupFile to your jest config in `package.json`:
51
51
52
52
If you are using [Create-React-App](https://github.com/facebookincubator/create-react-app) (CRA), the code for `setupTest.js` above should be placed into `src/setupTests.js` in the root of your project. CRA automatically uses this filename by convention in the Jest configuration it generates. Similarly, changing to your `package.json` is not required as CRA handles this when generating your Jest configuration.
53
53
54
+
### For Ejected Create React Apps _ONLY_:
55
+
54
56
> Note: Keep in mind that if you decide to "eject" before creating src/setupTests.js, the resulting package.json file won't contain any reference to it, so you should manually create the property setupTestFrameworkScriptFile in the configuration for Jest, something like the [following](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#srcsetuptestsjs-1):
55
57
56
58
```JSON
@@ -63,13 +65,13 @@ If you are using [Create-React-App](https://github.com/facebookincubator/create-
63
65
64
66
### Mock Responses
65
67
66
-
*`fetch.mockResponse(bodyOrFunction, init): fetch` - Mock all fetch calls
67
-
*`fetch.mockResponseOnce(bodyOrFunction, init): fetch` - Mock each fetch call independently
68
-
*`fetch.once(bodyOrFunction, init): fetch` - Alias for mockResponseOnce
*`fetch.resetMocks()` - Clear previously set mocks so they do not bleed into other mocks
99
-
*`fetch.mock` - The mock state for your fetch calls. Make assertions on the arguments given to `fetch` when called by the functions you are testing. For more information check the [Jest docs](https://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
99
+
-`fetch.resetMocks()` - Clear previously set mocks so they do not bleed into other mocks
100
+
-`fetch.mock` - The mock state for your fetch calls. Make assertions on the arguments given to `fetch` when called by the functions you are testing. For more information check the [Jest docs](https://facebook.github.io/jest/docs/en/mock-functions.html#mock-property)
100
101
101
102
For information on the arguments body and init can take, you can look at the MDN docs on the Response Constructor function, which `jest-fetch-mock` uses under the surface.
0 commit comments