Skip to content

Commit

Permalink
Merge pull request #788 from wheresrhys/rhys/mocking-in-core-docs
Browse files Browse the repository at this point in the history
docs: merge fetchHandler docs with mocking and spying page
  • Loading branch information
wheresrhys authored Aug 8, 2024
2 parents 96b24a8 + e5586dd commit f114903
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
14 changes: 0 additions & 14 deletions docs/docs/@fetch-mock/core/fetchHandler.md

This file was deleted.

24 changes: 16 additions & 8 deletions docs/docs/@fetch-mock/core/mocking-and-spying.md
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()`
2 changes: 2 additions & 0 deletions docs/docs/@fetch-mock/core/more-routing-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Specifies how to respond to calls to `fetch` that don't match any routes.

It accepts any [response](#api-mockingmock_response) compatible with `.route()`. If no argument is passed, then every unmatched call will receive a `200` response.

See also [.spy()](/fetch-mock/docs/@fetch-mock/core/mocking-and-spying#spymatcher-name)

## .sticky()

Shorthand for `mock()` which creates a route that persists even when `restore()`, `reset()` or `resetbehavior()` are called;
Expand Down

0 comments on commit f114903

Please sign in to comment.