Socket
Socket
Sign inDemoInstall

jest-fetch-mock

Package Overview
Dependencies
10
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.7.5 to 2.0.0

2

package.json
{
"name": "jest-fetch-mock",
"version": "1.7.5",
"version": "2.0.0",
"description": "fetch mock for jest",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -68,10 +68,27 @@ # Jest Fetch Mock

- `fetch.mockResponse(body, init): fetch` - Mock all fetch calls
- `fetch.mockResponseOnce(body, init): fetch` - Mock each fetch call independently
- `fetch.once(body, init): fetch` - Alias for mockResponseOnce
- `fetch.mockResponse(bodyOrFunction, init): fetch` - Mock all fetch calls
- `fetch.mockResponseOnce(bodyOrFunction, init): fetch` - Mock each fetch call independently
- `fetch.once(bodyOrFunction, init): fetch` - Alias for mockResponseOnce
- `fetch.mockResponses(...responses): fetch` - Mock multiple fetch calls independently
- Each argument is an array taking `[body, init]`
- `fetch.mockReject(error): fetch` - Mock all fetch calls, letting them fail directly
- `fetch.mockRejectOnce(error): fetch` - Let the next fetch call fail directly
- Each argument is an array taking `[bodyOrFunction, init]`
- `fetch.mockReject(errorOrFunction): fetch` - Mock all fetch calls, letting them fail directly
- `fetch.mockRejectOnce(errorOrFunction): fetch` - Let the next fetch call fail directly
### Functions
Instead of passing body, it is also possible to pass a function that returns a promise.
The promise should resolve with an object containing body and init props
i.e:
```
fetch.mockResponse(() => callMyApi().then(res => ({body: res}))
```
The same goes for rejects:
```
fetch.mockReject(() => doMyAsyncJob().then(res => Promise.reject(res.errorToRaise)))
```
### Mock utilities

@@ -78,0 +95,0 @@

@@ -13,8 +13,8 @@ declare module "jest-fetch-mock" {

(input?: string | Request, init?: RequestInit): Promise<Response>;
mockResponse(body: string, init?: MockParams): Fetch;
mockResponseOnce(body: string, init?: MockParams): Fetch;
mockResponse(body: string | Function, init?: MockParams): Fetch;
mockResponseOnce(body: string | Function, init?: MockParams): Fetch;
once(body: string, init?: MockParams): Fetch;
mockResponses(...responses : Array<[string] | [string, MockParams]>): Fetch;
mockReject(error?: Error): Fetch;
mockRejectOnce(error?: Error): Fetch;
mockReject(error?: Error | Function): Fetch;
mockRejectOnce(error?: Error | Function): Fetch;
resetMocks(): void;

@@ -21,0 +21,0 @@ }

@@ -39,2 +39,12 @@ const crossFetch = require('cross-fetch')

const isFn = unknown => typeof unknown === 'function'
const normalizeResponse = (bodyOrFunction, init) => () => isFn(bodyOrFunction) ?
bodyOrFunction().then(({body, init}) => new ResponseWrapper(body, init)) :
Promise.resolve(new ResponseWrapper(bodyOrFunction, init))
const normalizeError = errorOrFunction => isFn(errorOrFunction) ?
errorOrFunction :
() => Promise.reject(errorOrFunction)
const fetch = jest.fn()

@@ -44,17 +54,7 @@ fetch.Headers = Headers

fetch.Request = Request
fetch.mockResponse = (body, init) => {
return fetch.mockImplementation(() =>
Promise.resolve(new ResponseWrapper(body, init))
)
}
fetch.mockResponse = (bodyOrFunction, init) => fetch.mockImplementation(normalizeResponse(bodyOrFunction, init))
fetch.mockReject = error => {
return fetch.mockImplementation(() => Promise.reject(error))
}
fetch.mockReject = errorOrFunction => fetch.mockImplementation(normalizeError(errorOrFunction))
const mockResponseOnce = (body, init) => {
return fetch.mockImplementationOnce(() =>
Promise.resolve(new ResponseWrapper(body, init))
)
}
const mockResponseOnce = (bodyOrFunction, init) => fetch.mockImplementationOnce(normalizeResponse(bodyOrFunction, init))

@@ -65,12 +65,6 @@ fetch.mockResponseOnce = mockResponseOnce

fetch.mockRejectOnce = error => {
return fetch.mockImplementationOnce(() => Promise.reject(error))
}
fetch.mockRejectOnce = errorOrFunction => fetch.mockImplementationOnce(normalizeError(errorOrFunction))
fetch.mockResponses = (...responses) => {
responses.forEach(([body, init]) => {
fetch.mockImplementationOnce(() =>
Promise.resolve(new ResponseWrapper(body, init))
)
})
responses.forEach(([bodyOrFunction, init]) => fetch.mockImplementationOnce(normalizeResponse(bodyOrFunction, init)))
return fetch

@@ -77,0 +71,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc