Socket
Socket
Sign inDemoInstall

fetch-mock

Package Overview
Dependencies
2
Maintainers
1
Versions
213
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.3 to 1.2.0

2

package.json
{
"name": "fetch-mock",
"version": "1.1.3",
"version": "1.2.0",
"description": "Mock http requests made using fetch (or isomorphic-fetch)",

@@ -5,0 +5,0 @@ "main": "server.js",

@@ -5,4 +5,4 @@ # fetch-mock [![Build Status](https://travis-ci.org/wheresrhys/fetch-mock.svg?branch=master)](https://travis-ci.org/wheresrhys/fetch-mock) [![Coverage Status](https://coveralls.io/repos/wheresrhys/fetch-mock/badge.svg)](https://coveralls.io/r/wheresrhys/fetch-mock)

*notes*
- When using isomorphic-fetch or node-fetch fetch should be added as a global
- fetch-mock doesn't declare fetch or Promise as dependencies; as you're testing `fetch` it's assumed you're already taking care of these globals
- When using isomorphic-fetch or node-fetch `fetch` should be added as a global
- fetch-mock doesn't declare `fetch` or `Promise` as dependencies; as you're testing `fetch` it's assumed you're already taking care of these globals
- If you prefer documentation by example skip to the bottom of this README

@@ -15,5 +15,5 @@

### `mock(config)`
Replaces `fetch()` with a sinon stub which, in addition to the default sinon behaviour, records each of its calls and optionally returns a stub response or passes the call through to `fetch()`. `config` is an optional* object with the following properties.
Replaces `fetch()` with a sinon stub which, in addition to the default sinon behaviour, records it's calls, grouped by route, and optionally returns a stub response or passes the call through to `fetch()`. `config` is an optional* object with the following properties.
* `routes`: Either a single object or an array of similar objects each defining how the mock handles a given request. Each route object must have the following properties. If multiple routes are specified the first matching route will be used to define the response
* `routes`: Either a single object or an array of similar objects each defining how the mock handles a given request. If multiple routes are specified the first matching route will be used to define the response. Each route object must have the following properties.
* `name`: A unique string naming the route

@@ -23,7 +23,8 @@ * `matcher`: The rule for matching calls to `fetch()`. Accepts any of the following

* `RegExp`: A regular expression to test the url against
* `Function(url, opts)`: A function that is passed the url and opts `fetch` is called with and that returns a Boolean
* `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns a Boolean
* `response`: Configures the response object returned by the mock. Can take any of the following values
* `number`: creates a response with the number as the response status
* `string`: creates a 200 response with the string as the response body
* `object`: If the object contains any of the properties body, status, headers, throws; then these properties - all of them optional - are used to construct a response as follows
* `body`: Retunred in the response body
* `body`: Returned in the response body
* `status`: Returned in the response status

@@ -74,3 +75,3 @@ * `headers`: Returned in the response headers. They should be defined as an object literal (property names case-insensitive) which will be converted to a `Headers` instance

### `registerRoute(name, matcher, response)`
Often your application/module will always need responses for some calls in order to initialise properly, even if the content of those calls are not the subject of a given test e.g. a mock response from an authentication service and a lti-variant testing service might be necessary in order to test the UI for a version of a log in form. It's helpful to be able to define some default responses for these services which will exist throughout all or a large subset of your tests. `registerRoute()` aims to fulfil this need. All these predefined routes can be overridden when `mock(config)` is called.
Often your application/module will need a mocked response for some http requests in order to initialise properly, even if the content of those calls are not the subject of a given test e.g. a mock response from an authentication service and a multi-variant testing service might be necessary in order to test the UI for a version of a log in form. It's helpful to be able to define some default responses for these services which will exist throughout all or a large subset of your tests. `registerRoute()` aims to fulfil this need. All these predefined routes can be overridden when `mock(config)` is called.

@@ -85,4 +86,2 @@ `registerRoute()` takes either of the following parameters

## Example

@@ -164,3 +163,2 @@ ```javascript

after(function () {
// I wonder what this does??
fetchMock.unregisterRoute('content');

@@ -167,0 +165,0 @@ })

@@ -13,4 +13,8 @@ 'use strict';

// allow just body to be passed in as this is the commonest use case
if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
if (typeof config === 'number') {
config = {
status: config
};
} else if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
config = {
body: config

@@ -17,0 +21,0 @@ };

@@ -336,2 +336,18 @@ 'use strict';

describe('responses', function () {
it('respond with a status', function (done) {
fetchMock.mock({
routes: {
name: 'route',
matcher: 'http://it.at.there',
response: 300
}
});
fetch('http://it.at.there')
.then(function (res) {
expect(res.status).to.equal(300);
done();
});
});
it('respond with a string', function (done) {

@@ -338,0 +354,0 @@ fetchMock.mock({

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc