Skip to content

Commit

Permalink
docs: bring vitest docs into website
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Oct 20, 2024
1 parent 7f61c16 commit 22b549b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 66 deletions.
4 changes: 2 additions & 2 deletions docs/docs/API/CallHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ An options object compatible with the [route api](#api-mockingmock_options) to b

`fetchMock.callHistory` exposes the following methods.

> Note that fetch calls made using `(url, options)` pairs are added synchronously, but calls using a `Request` are added asynchronously. This is because when a `Request` is used access to many of its internals is via asynchronous methods, while for an options object they can be read directly. In general it's best to `await` your code to complete before attempting to access call history.
> Note that fetch calls made using `(url, options)` pairs are added synchronously, but calls using a `Request` are added asynchronously. This is because when a `Request` is used access to many of its internals is via asynchronous methods, while for an options object they can be read directly. In general it's best to `await` your application code to complete before attempting to access call history.
### .recordCall(callLog)

Expand All @@ -85,7 +85,7 @@ Returns a Boolean indicating whether `fetch` was called the expected number of t

A single name or an array of names can be passed in the `routeNames` parameter to check only certain routes. If no routeNames are passed it runs over all routes.

### .flush(waitForResponseMethods)
### .flush(waitForBody)

Returns a `Promise` that resolves once all fetches handled by fetch-mock have resolved

Expand Down
88 changes: 88 additions & 0 deletions docs/docs/wrappers/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,91 @@ sidebar_position: 1
---

# @fetch-mock/vitest

A wrapper for [fetch-mock](/fetch-mock/docs/) that improves the developer experience when working with vitest. It provides the following:

- Adds methods to fetchMock which wrap its default methods, but align more closely with vitest's naming conventions.
- Extends `expect` with convenience methods allowing for expressive tests such as `expect(fetchMock).toHavePosted('http://example.com', {id: 'test-id'})`.
- Can optionally be hooked in to vitest's global mock management methods such as `clearAllMocks()`.

## Requirements

@fetch-mock/vitest requires either of the following to run:

- [vitest](https://vitest.dev/guide/)
- The `fetch` API, via one of the following:
- [Node.js](https://nodejs.org/) 18+ for full feature operation
- Any modern browser that supports the `fetch` API
- [node-fetch](https://www.npmjs.com/package/node-fetch) when testing in earlier versions of Node.js (this is untested, but should mostly work)

## Installation

```shell
npm i -D @fetch-mock/vitest
```

## Setup

```js
import fetchMock, { manageFetchMockGlobally } from '@fetch-mock/vitest';

manageFetchMockGlobally(); // optional
```

## API

### fetchMock

An instance of [fetch-mock](/fetch-mock/docs/), with the following methods added:

#### fetchMock.mockClear()

Clears all call history from the mocked `fetch` implementation.

#### fetchMock.mockReset()

`fetchMock.mockReset({includeSticky: boolean})`

Clears all call history from the mocked `fetch` implementation _and_ removes all routes (including fallback routes defined using `.spy()` or `.catch()`) with the exception of sticky routes. To remove these, pass in the `includeSticky: true` option. FOr more fine grained control over fallback routes and named routes please use `fetchMock.removeRoutes()`

#### fetchMock.mockRestore()

`fetchMock.mockRestore({includeSticky: boolean})`

Calls `mockReset()` and additionally restores global fetch to its unmocked implementation.

### manageFetchMockGlobally()

Hooks fetchMock up to vitest's global mock management so that

- `vi.clearAllMocks()` will call `fetchMock.mockClear()`
- `vi.resetAllMocks()` will call `fetchMock.mockReset()`
- `vi.restoreAllMocks()` will call `fetchMock.mockRestore()`
- `vi.unstubAllGlobals()` will also call `fetchMock.mockRestore()`

Note that these **will not** clear any sticky routes added to fetchMock. You will need to make an additional call to `fetchMock.removeRoutes({includeSticky: true})`.

### Expect extensions

These are added to vitest automatically and are available on any expect call that is passed fetchMock as an argument. Their behaviour is similar to the vitest expectation methods mentioned in the comments below

```js
expect(fetchMock).toHaveFetched(filter, options); // .toHaveBeenCalled()/.toHaveBeenCalledWith()
expect(fetchMock).toHaveLastFetched(filter, options); // .toHaveBeenLastCalledWith()
expect(fetchMock).toHaveNthFetched(n, filter, options); // .toHaveBeenNthCalled()/.toHaveBeenNthCalledWith()
expect(fetchMock).toHaveFetchedTimes(n, filter, options); // .toHaveBeenCalledTimes()
expect(fetchMock).toBeDone(filter);
```

### Notes

- `filter` and `options` are the same as those used by `fetchMock.callHistory.calls()`.
- Each method can be prefixed with the `.not` helper for negative assertions. e.g. `expect(fetchMock).not.toBeDone('my-route')`
- In each of the method names `Fetched` can be replaced by any of the following verbs to scope to a particular method:
- Got
- Posted
- Put
- Deleted
- FetchedHead
- Patched
e.g. `expect(fetchMock).toHaveDeleted('http://example.com/user/1')`
69 changes: 5 additions & 64 deletions packages/vitest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,11 @@ A wrapper for fetch-mock that improves the developer experience when working wit
- Any modern browser that supports the `fetch` API
- [node-fetch](https://www.npmjs.com/package/node-fetch) when testing in earlier versions of Node.js (this is untested, but should mostly work)

## Installation
## Documentation and Usage

```shell
npm i -D @fetch-mock/vitest
```
See the [project website](https://www.wheresrhys.co.uk/fetch-mock/docs/wrappers/vitest/)

## Setup
## License

```js
import fetchMock, { manageFetchMockGlobally } from '@fetch-mock/vitest';

manageFetchMockGlobally(); // optional
```

## API

### fetchMock

An instance of [@fetch-mock/core](https://www.wheresrhys.co.uk/fetch-mock/docs/@fetch-mock/core/), with the following methods added:

#### fetchMock.mockClear()

Clears all call history from the mocked `fetch` implementation.

#### fetchMock.mockReset({includeSticky: boolean})

Clears all call history from the mocked `fetch` implementation _and_ removes all routes (including fallback routes defined using `.spy()` or `.catch()`) with the exception of sticky routes. To remove these, pass in the `includeSticky: true` option. FOr more fine grained control over fallback routes and named routes please use `fetchMock.removeRoutes()`

#### fetchMock.mockRestore({includeSticky: boolean})

Calls `mockReset()` and additionally restores global fetch to its unmocked implementation.

### manageFetchMockGlobally()

Hooks fetchMock up to vitest's global mock management so that

- `vi.clearAllMocks()` will call `fetchMock.mockClear()`
- `vi.resetAllMocks()` will call `fetchMock.mockReset()`
- `vi.restoreAllMocks()` will call `fetchMock.mockRestore()`
- `vi.unstubAllGlobals()` will also call `fetchMock.mockRestore()`

Note that these **will not** clear any sticky routes added to fetchMock. You will need to make an additional call to `fetchMock.removeRoutes({includeSticky: true})`.

### Expect extensions

These are added to vitest automatically and are available on any expect call that is passed fetchMock as an argument. Their behaviour is similar to the vitest expectation methods mentioned in the comments below

```js
expect(fetchMock).toHaveFetched(filter, options); // .toHaveBeenCalled()/.toHaveBeenCalledWith()
expect(fetchMock).toHaveLastFetched(filter, options); // .toHaveBeenLastCalledWith()
expect(fetchMock).toHaveNthFetched(n, filter, options); // .toHaveBeenNthCalled()/.toHaveBeenNthCalledWith()
expect(fetchMock).toHaveFetchedTimes(n, filter, options); // .toHaveBeenCalledTimes()
expect(fetchMock).toBeDone(filter);
```

### Notes

- `filter` and `options` are the same as those used by `fetchMock.callHistory.calls()`.
- Each method can be prefixed with the `.not` helper for negative assertions. e.g. `expect(fetchMock).not.toBeDone('my-route')`
- In each of the method names `Fetched` can be replaced by any of the following verbs to scope to a particular method:
- Got
- Posted
- Put
- Deleted
- FetchedHead
- Patched
e.g. `expect(fetchMock).toHaveDeleted('http://example.com/user/1')`
@fetch-mock/vitest is licensed under the [MIT](https://github.com/wheresrhys/fetch-mock/blob/master/LICENSE) license.
Copyright © 2024, Rhys Evans

0 comments on commit 22b549b

Please sign in to comment.