-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
106bacf
commit 93f35c6
Showing
12 changed files
with
1,133 additions
and
284 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
export default { | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+.tsx?$": ["ts-jest",{}], | ||
}, | ||
moduleNameMapper: { | ||
'@fetch-mock/core': '<rootDir>/packages/core' | ||
} | ||
}; |
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Rhys Evans | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# @fetch-mock/jest | ||
|
||
A wrapper for fetch-mock that improves the developer experience when working with jest. It provides the following: | ||
|
||
- Adds methods to fetchMock which wrap its default methods, but align more closely with jest'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 jest's global mock management methods such as `clearAllMocks()`. | ||
|
||
## Requirements | ||
|
||
@fetch-mock/jest requires either of the following to run: | ||
|
||
- [jest](https://jest.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/jest | ||
``` | ||
|
||
## Setup | ||
|
||
```js | ||
import fetchMock, { manageFetchMockGlobally } from '@fetch-mock/jest'; | ||
|
||
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 jest's global mock management so that | ||
|
||
- `vi.clearAllMocks()` will call `fetchMock.mockClear()` | ||
- `vi.resetAllMocks()` will call `fetchMock.mockReset()` | ||
- `vi.restoreAllMocks()` will 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 jest automatically and are available on any expect call that is passed fetchMock as an argument. Their behaviour is similar to the jest 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')` |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@fetch-mock/jest", | ||
"description": "jest wrapper for fetch-mock", | ||
"version": "0.1.0", | ||
"exports": { | ||
"browser": "./src/index.js", | ||
"import": "./src/index.js", | ||
"require": "./dist/commonjs.js", | ||
"types": "./types/index.d.ts" | ||
}, | ||
"main": "./dist/commonjs.js", | ||
"module": "./src/index.js", | ||
"types": "./types/index.d.ts", | ||
"type": "module", | ||
"engines": { | ||
"node": ">=18.11.0" | ||
}, | ||
"dependencies": { | ||
"@fetch-mock/core": "^0.7.0" | ||
}, | ||
"peerDependencies": { | ||
"jest": "*", | ||
"@jest/globals": "*" | ||
}, | ||
"repository": { | ||
"directory": "packages/jest", | ||
"type": "git", | ||
"url": "git+https://github.com/wheresrhys/fetch-mock.git" | ||
}, | ||
"scripts": { | ||
"build": "rm -rf dist && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node ../../scripts/declare-dist-type.js" | ||
}, | ||
"license": "MIT", | ||
"author": "Rhys Evans", | ||
"bugs": { | ||
"url": "https://github.com/wheresrhys/fetch-mock/issues" | ||
}, | ||
"homepage": "http://www.wheresrhys.co.uk/fetch-mock", | ||
"keywords": [ | ||
"fetch", | ||
"http", | ||
"mock", | ||
"testing", | ||
"spy", | ||
"stub", | ||
"jest" | ||
] | ||
} |
Oops, something went wrong.