Socket
Socket
Sign inDemoInstall

fetch-mock

Package Overview
Dependencies
Maintainers
1
Versions
226
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-mock - npm Package Compare versions

Comparing version 2.4.0 to 3.0.0

62

es5/fetch-mock.js

@@ -83,6 +83,2 @@ 'use strict';

if (!route.name) {
throw 'each route must be named';
}
if (!route.matcher) {

@@ -92,2 +88,7 @@ throw 'each route must specify a string, regex or function to match calls to fetch';

if (!route.name) {
route.name = route.matcher.toString();
route.__unnamed = true;
}
if (typeof route.response === 'undefined') {

@@ -225,3 +226,3 @@ throw 'each route must define a response';

debug('no one time only routes defined. Using preregistered routes only');
routes = this.routes;
routes = [].slice.call(this.routes);
}

@@ -239,3 +240,3 @@

return function (url, opts) {
var router = function router(url, opts) {
var response = undefined;

@@ -268,2 +269,8 @@ debug('searching for matching route for ' + url);

};
router.augment = function (additionalRoutes) {
routes = routes.concat(additionalRoutes.map(compileRoute));
};
return router;
}

@@ -273,4 +280,9 @@ }, {

value: function push(name, call) {
this._calls[name] = this._calls[name] || [];
this._calls[name].push(call);
if (name) {
this._calls[name] = this._calls[name] || [];
this._calls[name].push(call);
this._matchedCalls.push(call);
} else {
this._unmatchedCalls.push(call);
}
}

@@ -286,3 +298,2 @@ }, {

routes: [{
name: '_mock',
matcher: matcher,

@@ -296,3 +307,2 @@ method: method,

routes: [{
name: '_mock',
matcher: matcher,

@@ -317,8 +327,12 @@ response: method

if (this.isMocking) {
throw 'fetch-mock is already mocking routes. Call .restore() before mocking again or use .reMock() if this is intentional';
this.mockedContext.fetch.augment(config.routes);
return this;
}
this.isMocking = true;
this._matchedCalls = [];
this._unmatchedCalls = [];
return this.mockedContext.fetch = this.constructMock(config);
this.mockedContext.fetch = this.constructMock(config);
return this;
}

@@ -335,3 +349,3 @@ }, {

return function (url, opts) {
var mock = function mock(url, opts) {
var response = router(url, opts);

@@ -343,3 +357,3 @@ if (response) {

debug('response not found for ' + url);
_this2.push('__unmatched', [url, opts]);
_this2.push(null, [url, opts]);
if (config.greed === 'good') {

@@ -357,2 +371,8 @@ debug('sending default good response');

};
mock.augment = function (routes) {
router.augment(routes);
};
return mock;
}

@@ -375,2 +395,7 @@ }, {

}, {
key: 'getMock',
value: function getMock() {
return this.fetch;
}
}, {
key: 'reset',

@@ -380,2 +405,4 @@ value: function reset() {

this._calls = {};
this._matchedCalls = [];
this._unmatchedCalls = [];
}

@@ -385,3 +412,6 @@ }, {

value: function calls(name) {
return name ? this._calls[name] || [] : this._calls._mock || this._calls;
return name ? this._calls[name] || [] : {
matched: this._matchedCalls,
unmatched: this._unmatchedCalls
};
}

@@ -392,3 +422,3 @@ }, {

if (!name) {
return !!Object.keys(this._calls).length;
return !!this._matchedCalls.length;
}

@@ -395,0 +425,0 @@ return !!(this._calls[name] && this._calls[name].length);

{
"name": "fetch-mock",
"version": "2.4.0",
"version": "3.0.0",
"description": "Mock http requests made using fetch (or isomorphic-fetch)",

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

# 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)
Mock http requests made using fetch (or [isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch)). As well as shorthand methods for the simplest use cases, it offers a flexible API for customising mocking behaviour, and can also be persisted (with resettable state) over a series of tests.
## Which version to require
- [V3 changelog](https://github.com/wheresrhys/fetch-mock/pull/35)
## Which file to require
- Browser or nodejs 4 or higher `require('fetch-mock')`

@@ -18,3 +20,3 @@ - Browser tests when not using a loader that respects the `browser` field of package.json: `require('fetch-mock/es5/client')`

#### `mock(matcher, response)` or `mock(matcher, method, response)`
Replaces `fetch()` with a stub which records it's calls, grouped by route, and optionally returns a mocked `Response` object or passes the call through to `fetch()`.
Replaces `fetch()` with a stub which records it's calls, grouped by route, and optionally returns a mocked `Response` object or passes the call through to `fetch()`. Calls to `.mock()` can be chained.

@@ -36,3 +38,11 @@ * `matcher` [required]: Condition for selecting which requests to mock Accepts any of the following

##### Example
```
const fetchMock = require('fetch-mock');
fetchMock
.mock('http://domain1', 200)
.mock('http://domain2', 'DELETE', 204);
```
#### `restore()`

@@ -42,3 +52,3 @@ Restores `fetch()` to its unstubbed state and clears all data recorded for its calls

#### `reMock()`
Normally calling `mock()` twice without restoring inbetween will throw an error. `reMock()` calls `restore()` internally before calling `mock()` again. This allows you to put a generic call to `mock()` in a `beforeEach()` while retaining the flexibility to vary the responses for some tests
Calls `restore()` internally then calls `mock()`. This allows you to put some generic calls to `mock()` in a `beforeEach()` while retaining the flexibility to vary the responses for some tests

@@ -48,4 +58,7 @@ #### `reset()`

#### `calls()`
Returns an array of arrays of the arguments passed to `fetch()` for mocked calls.
#### `calls(matcher)`
Returns an object `{matched: [], unmatched: []}` containing arrays of all calls to fetch, grouped by whether fetch-mock matched them or not. If `matcher` is specified and is equal to `matcher.toString()` for any of the mocked routes then only calls to fetch matching that route are returned.
#### `called(matcher)`
Returns a Boolean indicating whether fetch was called and a route was matched. If `matcher` is specified and is equal to `matcher.toString()` for any of the mocked routes then only returns `true` if that particular route was matched.

@@ -57,3 +70,3 @@ ## Advanced usage

Use a configuration object to define a route to mock.
* `name` [required]: A unique string naming the route. Used to subsequently retrieve references to the calls, grouped by name
* `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

@@ -88,8 +101,7 @@ * `matcher` [required]: as specified above

#### `calls(routeName)`
Returns an array of arrays of the arguments passed to `fetch()` that matched the given route. '__unmatched' can be passed in to return results for calls not matching any route.
Returns an array of arrays of the arguments passed to `fetch()` that matched the given route.
#### `called(routeName)`
Returns a Boolean denoting whether any calls matched the given route. '__unmatched' can be passed in to return results for calls not matching any route. If no routeName is passed it returns `true` if any fetch calls were made
Returns a Boolean denoting whether any calls matched the given route.

@@ -110,2 +122,4 @@ #### `registerRoute()`

Since fetch-mock v3 calls to `mock()` are chainable, so to obtain a reference to the mocked fetch call `getMock()`.
##### Mockery example

@@ -122,3 +136,3 @@ ```javascript

it('should make a request', function (done) {
mockery.registerMock('fetch', fetchMock.mock());
mockery.registerMock('fetch', fetchMock.mock().getMock());
// test code goes in here

@@ -125,0 +139,0 @@ mockery.deregisterMock('fetch');

@@ -75,6 +75,2 @@ 'use strict';

if (!route.name) {
throw 'each route must be named';
}
if (!route.matcher) {

@@ -84,2 +80,7 @@ throw 'each route must specify a string, regex or function to match calls to fetch';

if (!route.name) {
route.name = route.matcher.toString();
route.__unnamed = true;
}
if (typeof route.response === 'undefined') {

@@ -200,3 +201,3 @@ throw 'each route must define a response';

debug('no one time only routes defined. Using preregistered routes only');
routes = this.routes;
routes = [].slice.call(this.routes);
}

@@ -215,3 +216,3 @@

return (url, opts) => {
const router = (url, opts) => {
let response;

@@ -244,7 +245,18 @@ debug('searching for matching route for ' + url);

};
router.augment = function (additionalRoutes) {
routes = routes.concat(additionalRoutes.map(compileRoute));
}
return router;
}
push (name, call) {
this._calls[name] = this._calls[name] || [];
this._calls[name].push(call);
if (name) {
this._calls[name] = this._calls[name] || [];
this._calls[name].push(call);
this._matchedCalls.push(call);
} else {
this._unmatchedCalls.push(call);
}
}

@@ -259,3 +271,2 @@

routes: [{
name: '_mock',
matcher,

@@ -270,3 +281,2 @@ method,

routes: [{
name: '_mock',
matcher,

@@ -292,8 +302,12 @@ response: method

if (this.isMocking) {
throw 'fetch-mock is already mocking routes. Call .restore() before mocking again or use .reMock() if this is intentional';
this.mockedContext.fetch.augment(config.routes);
return this;
}
this.isMocking = true;
this._matchedCalls = [];
this._unmatchedCalls = [];
return this.mockedContext.fetch = this.constructMock(config);
this.mockedContext.fetch = this.constructMock(config);
return this;
}

@@ -307,3 +321,3 @@

return (url, opts) => {
const mock = (url, opts) => {
const response = router(url, opts);

@@ -315,3 +329,3 @@ if (response) {

debug('response not found for ' + url);
this.push('__unmatched', [url, opts]);
this.push(null, [url, opts]);
if (config.greed === 'good') {

@@ -329,2 +343,8 @@ debug('sending default good response');

};
mock.augment = function (routes) {
router.augment(routes);
}
return mock;
}

@@ -345,9 +365,18 @@

getMock () {
return this.fetch;
}
reset () {
debug('resetting call logs');
this._calls = {};
this._matchedCalls = [];
this._unmatchedCalls = [];
}
calls (name) {
return name ? (this._calls[name] || []) : (this._calls._mock || this._calls);
return name ? (this._calls[name] || []) : {
matched: this._matchedCalls,
unmatched: this._unmatchedCalls
};
}

@@ -357,3 +386,3 @@

if (!name) {
return !!Object.keys(this._calls).length;
return !!(this._matchedCalls.length);
}

@@ -360,0 +389,0 @@ return !!(this._calls[name] && this._calls[name].length);

@@ -33,3 +33,3 @@ 'use strict';

expect(fetchMock.realFetch).to.equal(dummyFetch);
var mock = fetchMock.mock();
var mock = fetchMock.mock().getMock();
expect(typeof mock).to.equal('function');

@@ -39,3 +39,3 @@ expect(function () {

}).not.to.throw();
expect(fetchMock.called('__unmatched')).to.be.true;
expect(fetchMock.calls().unmatched.length).to.equal(1);
expect(fetchCalls.length).to.equal(1);

@@ -42,0 +42,0 @@ expect(fetchCalls[0]).to.eql(['url', {prop: 'val'}]);

@@ -43,10 +43,25 @@ 'use strict';

it('throw() if attempting to mock more than once', function () {
fetchMock.mock();
it('allow multiple mocking calls', function () {
fetchMock.mock('^http://route1', 200);
expect(function () {
fetchMock.mock();
}).to.throw();
fetchMock.mock('^http://route2', 200);
}).not.to.throw();
fetch('http://route1.com')
fetch('http://route2.com')
expect(fetchMock.calls().matched.length).to.equal(2);
fetchMock.restore();
});
it('mocking is chainable', function () {
expect(function () {
fetchMock
.mock('^http://route1', 200)
.mock('^http://route2', 200);
}).not.to.throw();
fetch('http://route1.com')
fetch('http://route2.com')
expect(fetchMock.calls().matched.length).to.equal(2);
fetchMock.restore();
});
it('allow remocking after being restored', function () {

@@ -61,2 +76,12 @@ fetchMock.mock();

it('have remocking helper', function () {
fetchMock.mock('^http://route1', 200)
expect(function () {
fetchMock.reMock('^http://route2', 200)
}).not.to.throw();
fetch('http://route1.com')
fetch('http://route2.com')
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls().matched[0][0]).to.equal('http://route2.com');
});
});

@@ -85,3 +110,6 @@

fetchMock.mock({
routes: [{name: 'route1', matcher: 'http://it.at.there', response: 'ok'}, {name: 'route2', matcher: 'http://it.at.there', response: 'ok'}]
routes: [
{name: 'route1', matcher: 'http://it.at.there', response: 'ok'},
{name: 'route2', matcher: 'http://it.at.there', response: 'ok'}
]
});

@@ -91,3 +119,3 @@ }).not.to.throw();

it('expects a name', function () {
it('falls back to matcher.toString() as a name', function () {
expect(function () {

@@ -97,3 +125,5 @@ fetchMock.mock({

});
}).to.throw();
}).not.to.throw();
fetch('http://it.at.there');
expect(fetchMock.calls('http://it.at.there').length).to.equal(1);
});

@@ -120,3 +150,6 @@

fetchMock.mock({
routes: [{name: 'route', matcher: 'http://it.at.there', response: 'ok'}, {name: 'route', matcher: 'http://it.at.there', response: 'ok'}]
routes: [
{name: 'route', matcher: 'http://it.at.there', response: 'ok'},
{name: 'route', matcher: 'http://it.at.here', response: 'ok'}
]
});

@@ -133,3 +166,4 @@ }).to.throw();

fetch('http://it.at.there', {method: 'PUT'});
expect(fetchMock.calls().length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls('http://it.at.there').length).to.equal(1);
});

@@ -142,3 +176,4 @@

fetch('http://it.at.there');
expect(fetchMock.calls().length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls('http://it.at.there').length).to.equal(1);
});

@@ -151,2 +186,3 @@

fetch('http://it.at.there');
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls('route').length).to.equal(1);

@@ -164,2 +200,3 @@ });

fetch('http://it.at.where');
expect(fetchMock.calls().matched.length).to.equal(2);
expect(fetchMock.calls('route1').length).to.equal(1);

@@ -174,7 +211,9 @@ expect(fetchMock.calls('route2').length).to.equal(1);

fetchMock.mock();
Promise.all([fetch('http://1', {method: 'GET'}), fetch('http://2', {method: 'POST'})])
Promise.all([
fetch('http://1', {method: 'GET'}),
fetch('http://2', {method: 'POST'})
])
.then(function () {
expect(fetchMock.called()).to.be.true;
expect(fetchMock.called('__unmatched')).to.be.true;
var unmatchedCalls = fetchMock.calls('__unmatched');
expect(fetchMock.called()).to.be.false;
var unmatchedCalls = fetchMock.calls().unmatched;
expect(unmatchedCalls.length).to.equal(2);

@@ -184,3 +223,4 @@ expect(unmatchedCalls[0]).to.eql(['http://1', {method: 'GET'}]);

done();
});
})
});

@@ -192,5 +232,4 @@

.then(function (res) {
expect(fetchMock.called()).to.be.true;
expect(fetchMock.called('__unmatched')).to.be.true;
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.called()).to.be.false;
expect(fetchMock.calls().unmatched.length).to.equal(1);
expect(res.status).to.equal(200);

@@ -208,4 +247,3 @@ res.text().then(function (text) {

.catch(function (res) {
expect(fetchMock.called()).to.be.true;
expect(fetchMock.called('__unmatched')).to.be.true;
expect(fetchMock.called()).to.be.false;
expect(res).to.equal('unmocked url: http://1');

@@ -220,8 +258,9 @@ done();

.then(function () {
expect(fetchMock.called()).to.be.true;
expect(fetchMock.called('__unmatched')).to.be.true;
expect(fetchMock.called()).to.be.false;
expect(fetchMock.calls().unmatched.length).to.equal(1);
expect(fetchCalls.length).to.equal(1);
expect(fetchCalls[0].length).to.equal(2);
done();
})
});
});

@@ -244,4 +283,5 @@

expect(fetchMock.called('route')).to.be.true;
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls('route').length).to.equal(1);
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.calls().unmatched.length).to.equal(1);
done();

@@ -259,8 +299,13 @@ });

});
Promise.all([fetch('http://it.at.there'), fetch('http://it.at.thereabouts'), fetch('http://it.at.hereabouts')])
Promise.all([
fetch('http://it.at.there'),
fetch('http://it.at.thereabouts'),
fetch('http://it.at.hereabouts')]
)
.then(function (res) {
expect(fetchMock.called()).to.be.true;
expect(fetchMock.called('route')).to.be.true;
expect(fetchMock.calls().matched.length).to.equal(2);
expect(fetchMock.calls('route').length).to.equal(2);
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.calls().unmatched.length).to.equal(1);
done();

@@ -283,3 +328,4 @@ });

expect(fetchMock.calls('route').length).to.equal(1);
expect(fetchMock.calls('__unmatched').length).to.equal(2);
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls().unmatched.length).to.equal(2);
done();

@@ -308,3 +354,4 @@ });

expect(fetchMock.calls('route').length).to.equal(1);
expect(fetchMock.calls('__unmatched').length).to.equal(2);
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls().unmatched.length).to.equal(2);
done();

@@ -335,3 +382,4 @@ });

expect(fetchMock.calls('route2').length).to.equal(1);
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(3);
expect(fetchMock.calls().unmatched.length).to.equal(1);
done();

@@ -360,3 +408,4 @@ }).catch(done);

expect(fetchMock.calls('route2').length).to.equal(1);
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(2);
expect(fetchMock.calls().unmatched.length).to.equal(1);
done();

@@ -383,2 +432,3 @@ });

expect(fetchMock.calls('route1').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(1);
expect(fetchMock.calls('route2').length).to.equal(0);

@@ -401,3 +451,3 @@ done();

expect(fetchMock.called('route')).to.be.true;
expect(fetchMock.calls().route).to.exist;
expect(fetchMock.calls().matched.length).to.equal(2);
expect(fetchMock.calls('route')[0]).to.eql(['http://it.at.there', undefined]);

@@ -423,2 +473,3 @@ expect(fetchMock.calls('route')[1]).to.eql(['http://it.at.thereabouts', {headers: {head: 'val'}}]);

expect(fetchMock.calls('route').length).to.equal(0);
expect(fetchMock.calls().matched.length).to.equal(0);
done();

@@ -442,2 +493,3 @@ });

expect(fetchMock.calls('route').length).to.equal(0);
expect(fetchMock.calls().matched.length).to.equal(0);
done();

@@ -599,2 +651,3 @@ });

expect(fetchMock.calls('route').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(1);
done();

@@ -614,2 +667,3 @@ });

expect(fetchMock.calls('route').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(1);
done();

@@ -634,2 +688,3 @@ });

expect(fetchMock.calls('route2').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(2);
done();

@@ -662,2 +717,3 @@ });

expect(fetchMock.calls('route2').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(2);
done();

@@ -687,3 +743,4 @@ })

expect(fetchMock.calls('route2').length).to.equal(0);
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.calls().unmatched.length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(1);
done();

@@ -714,3 +771,4 @@ });

expect(fetchMock.calls('route2').length).to.equal(0);
expect(fetchMock.calls('__unmatched').length).to.equal(2);
expect(fetchMock.calls().unmatched.length).to.equal(2);
expect(fetchMock.calls().matched.length).to.equal(1);
done();

@@ -760,3 +818,4 @@ });

expect(fetchMock.calls('route2').length).to.equal(0);
expect(fetchMock.calls('__unmatched').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(2);
expect(fetchMock.calls().unmatched.length).to.equal(1);
done();

@@ -783,2 +842,3 @@ });

expect(fetchMock.calls('route1').length).to.equal(1);
expect(fetchMock.calls().matched.length).to.equal(2);
done();

@@ -805,2 +865,3 @@ });

expect(fetchMock.calls('route1').length).to.equal(0);
expect(fetchMock.calls().matched.length).to.equal(1);
done();

@@ -807,0 +868,0 @@ });

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc