fetch-mock
Advanced tools
Comparing version 3.1.1 to 3.1.2
{ | ||
"name": "fetch-mock", | ||
"version": "3.1.1", | ||
"version": "3.1.2", | ||
"description": "Mock http requests made using fetch (or isomorphic-fetch)", | ||
@@ -5,0 +5,0 @@ "main": "src/server.js", |
@@ -63,6 +63,6 @@ # fetch-mock [![Build Status](https://travis-ci.org/wheresrhys/fetch-mock.svg?branch=master)](https://travis-ci.org/wheresrhys/fetch-mock) | ||
Use a configuration object to define a route to mock. | ||
* `name` [optional]: A unique string naming the route. Used to subsequently retrieve references to the calls, grouped by name. If not specified defaults to `matcher.toString()` | ||
* `method` [optional]: http method | ||
* `matcher` [required]: as specified above | ||
* `response` [required]: as specified above | ||
* `name` [optional]: A unique string naming the route. Used to subsequently retrieve references to the calls, grouped by name. If not specified defaults to `matcher.toString()` | ||
* `method` [optional]: http method | ||
* `matcher` [required]: as specified above | ||
* `response` [required]: as specified above | ||
@@ -73,23 +73,22 @@ #### `mock(routes)` | ||
#### `mock(config)` | ||
Pas in an object containing more complex config for fine grained control over every aspect of mocking behaviour. May have the following properties | ||
- `routes`: Either a single route config object or an array of them (see above) | ||
- `responses`: When `registerRoute()` (see below) has already been used to register some routes then `responses` can be used to override the default response. Its value should be an object mapping route names to responses, which should be similar to those provided in the `response` property of stanadard route configurations e.g. | ||
* `routes`: Either a single route config object or an array of them (see above) | ||
* `responses`: When `registerRoute()` (see below) has already been used to register some routes then `responses` can be used to override the default response. Its value should be an object mapping route names to responses, which should be similar to those provided in the `response` property of stanadard route configurations e.g. | ||
``` | ||
responses: { | ||
session: function (url, opts) { | ||
if (opts.headers.authorized) { | ||
return {user: 'dummy-authorized-user'}; | ||
} else { | ||
return {user: 'dummy-unauthorized-user'}; | ||
} | ||
} | ||
} | ||
``` | ||
* `greed`: Determines how the mock handles unmatched requests | ||
* 'none': all unmatched calls get passed through to `fetch()` | ||
* 'bad': all unmatched calls result in a rejected promise | ||
* 'good': all unmatched calls result in a resolved promise with a 200 status | ||
``` | ||
responses: { | ||
session: function (url, opts) { | ||
if (opts.headers.authorized) { | ||
return {user: 'dummy-authorized-user'}; | ||
} else { | ||
return {user: 'dummy-unauthorized-user'}; | ||
} | ||
} | ||
} | ||
``` | ||
- `greed`: Determines how the mock handles unmatched requests | ||
- 'none': all unmatched calls get passed through to `fetch()` | ||
- 'bad': all unmatched calls result in a rejected promise | ||
- 'good': all unmatched calls result in a resolved promise with a 200 status | ||
#### `calls(routeName)` | ||
@@ -115,3 +114,3 @@ Returns an array of arrays of the arguments passed to `fetch()` that matched the given route. | ||
Since fetch-mock v3 calls to `mock()` are chainable, so to obtain a reference to the mocked fetch call `getMock()`. | ||
To obtain a reference to the mock fetch call `getMock()`. | ||
@@ -139,9 +138,9 @@ ##### Mockery example | ||
- If your client-side code or tests do not use a loader that respects the browser field of package.json use `require('fetch-mock/es5/client')`. | ||
- If you need to use fetch-mock without commonjs, you can include the precompiled `node_modules/fetch-mock/es5/client-browserified.js` in a script tag. This loads fetch-mock into the `fetchMock` global variable. | ||
- For server side tests running in nodejs 0.12 or lower use `require('fetch-mock/es5/server')` | ||
* If your client-side code or tests do not use a loader that respects the browser field of package.json use `require('fetch-mock/es5/client')`. | ||
* If you need to use fetch-mock without commonjs, you can include the precompiled `node_modules/fetch-mock/es5/client-browserified.js` in a script tag. This loads fetch-mock into the `fetchMock` global variable. | ||
* For server side tests running in nodejs 0.12 or lower use `require('fetch-mock/es5/server')` | ||
### Polyfilling fetch | ||
- In nodejs `require('isomorphic-fetch')` before any of your tests. | ||
- In the browser `require('isomorphic-fetch')` can also be used, but it may be easier to `npm install whatwg-fetch` (the module isomorphic-fetch is built around) and load `./node_modules/whatwg-fetch/fetch.js` directly into the page, either in a script tag or by referencing it your test runner config | ||
* In nodejs `require('isomorphic-fetch')` before any of your tests. | ||
* In the browser `require('isomorphic-fetch')` can also be used, but it may be easier to `npm install whatwg-fetch` (the module isomorphic-fetch is built around) and load `./node_modules/whatwg-fetch/fetch.js` directly into the page, either in a script tag or by referencing it your test runner config | ||
@@ -148,0 +147,0 @@ ## Examples |
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
77719
283