fetch-mock-jest
Advanced tools
Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "fetch-mock-jest", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "fetch-mock-jest.js", |
# fetch-mock-jest | ||
Wrapper around fetch-mock to make working with jest a jolly old joy. | ||
Wrapper around [fetch-mock](http://www.wheresrhys.co.uk/fetch-mock) - a comprehensive, isomorphic mock for the [fetch api](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) - which provides an interface that is more idiomatic when working in [jest](https://jestjs.io) | ||
# Installation | ||
`npm install -D fetch-mock-jest` | ||
## global fetch | ||
@@ -14,3 +16,3 @@ | ||
``` | ||
jest.mock('node-fetch', () => require('fetch-mock-jest')) | ||
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox()) | ||
const fetchMock = require('node-fetch') | ||
@@ -21,9 +23,25 @@ ``` | ||
`expect(fetchMock)` can be inspected using all the built in jest function inspection assertions. `fetch.mockClear()` and `fetch.mockReset()` shodul aslo work (however all other jest mock setup methods are disabled as fetch-mock's own methdos should always be used) | ||
All the built in jest function inspection assertions can be used, e.g. `expect(fetchMock).toHaveBeenCalledWith('http://example.com')`. | ||
In addition, the following work: | ||
`expect(fetchMock).toHaveLastFetched(url, options)` | ||
`expect(fetchMock).toHaveNthFetched(n, url, options)` | ||
`expect(fetchMock).toHaveFetchedTimes(times, url, options)` | ||
`expect(fetchMock).toBeDone(matcher)` | ||
, proxying through to the similar fetchMock methods. | ||
`fetch.mockClear()` can be used to reset the call history | ||
`fetch.mockReset()` can be used to remove all configured mocks | ||
All other jest methods for configuring mock functions are disabled as fetch-mock's own methods should always be used | ||
`fetchMock.mock.calls` and `fetchMock.mock.results` are also exposed, giving access to manually inspect the calls. | ||
The following custom jest expectation methods, proxying through to `fetch-mock`'s inspection methods are also available. They can all be prefixed with the `.not` helper for negative assertions. | ||
- `expect(fetchMock).toHaveLastFetched(filter, options)` | ||
- `expect(fetchMock).toHaveNthFetched(n, filter, options)` | ||
- `expect(fetchMock).toHaveFetchedTimes(n, filter, options)` | ||
- `expect(fetchMock).toBeDone(filter)` | ||
`filter` and `options` are the same as those used by [`fetch-mock`'s inspection methods](http://www.wheresrhys.co.uk/fetch-mock/#api-inspectionfundamentals) | ||
### TODO | ||
These would also be useful jest extensions: | ||
- `toHaveFetched` | ||
- `toHaveRespondedWith({status, body, headers})` (using objectMatching semantics) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18918
46
72