-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #788 from wheresrhys/rhys/mocking-in-core-docs
docs: merge fetchHandler docs with mocking and spying page
- Loading branch information
Showing
3 changed files
with
18 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 1 | ||
--- | ||
|
||
# Mocking and spying | ||
|
||
These methods allow mocking or spying on the `fetch` implementation used by your application. | ||
|
||
Note that these methods are only implemented in `@fetch-mock/core` and are not avilable when using `@fetch-mock/jest`, `@fetch-mock/vitest` etc.... Those libraries provide ways to mock `fetch` that are more idiomatic to their own ecosystem. | ||
## fetchHandler | ||
|
||
## When using global fetch in your application | ||
`fetchMock.fetchHandler(url, requestInit)` | ||
|
||
### mockGlobal() | ||
A mock implementation of `fetch`. | ||
|
||
By default it will error. In order to return responses `.route()`, `.catch()` and other routing methods of `fetchMock` must first be used to add routes to its internal router. | ||
|
||
All calls made using `fetchMock.fetchHandler` are recorded in `fetchMock.callHistory`. | ||
|
||
You can either pass `fetchMock.fetchHandler` into your choice of mocking library or use the methods below to mock global `fetch`. | ||
|
||
## mockGlobal() | ||
|
||
Replaces `globalThis.fetch` with `fm.fetchHandler` | ||
|
||
### unmockGlobal() | ||
## unmockGlobal() | ||
|
||
Restores `globalThis.fetch` to its original state | ||
|
||
### spy(matcher, name) | ||
## spy(matcher, name) | ||
|
||
Falls back to the `fetch` implementation set in `fetchMock.config.fetch` for a specific route (which can be named). | ||
|
||
When no arguments are provided it will fallback to the native fetch implementation for all requests, similar to `.catch()` | ||
When no arguments are provided it will fallback to the native fetch implementation for all requests, similar to `.catch()`. | ||
|
||
### spyGlobal() | ||
## spyGlobal() | ||
|
||
Equivalent to calling `.mockGlobal()` followed by `.spy()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters