fetch-mock
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -40,2 +40,6 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.fetchMock = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
// It seems odd to call this in here even though it's already called within fetchMock | ||
// It's to handle the fact that because we want to support making it very easy to add a | ||
// delay to any sort of response (including responses which are defined with a function) | ||
// while also allowing function responses to return a Promise for a response config. | ||
if (typeof responseConfig === 'function') { | ||
@@ -281,3 +285,9 @@ responseConfig = responseConfig(url, fetchOpts); | ||
var response = this.router(url, opts); | ||
if (response) { | ||
if (typeof response === 'function') { | ||
response = response(url, opts); | ||
} | ||
if (response instanceof Promise) { | ||
@@ -284,0 +294,0 @@ return response.then(function (response) { |
@@ -25,2 +25,6 @@ 'use strict'; | ||
// It seems odd to call this in here even though it's already called within fetchMock | ||
// It's to handle the fact that because we want to support making it very easy to add a | ||
// delay to any sort of response (including responses which are defined with a function) | ||
// while also allowing function responses to return a Promise for a response config. | ||
if (typeof responseConfig === 'function') { | ||
@@ -266,3 +270,9 @@ responseConfig = responseConfig(url, fetchOpts); | ||
var response = this.router(url, opts); | ||
if (response) { | ||
if (typeof response === 'function') { | ||
response = response(url, opts); | ||
} | ||
if (response instanceof Promise) { | ||
@@ -269,0 +279,0 @@ return response.then(function (response) { |
{ | ||
"name": "fetch-mock", | ||
"version": "4.4.0", | ||
"version": "4.5.0", | ||
"description": "Mock http requests made using fetch (or isomorphic-fetch)", | ||
@@ -5,0 +5,0 @@ "main": "src/server.js", |
@@ -31,3 +31,3 @@ # fetch-mock [![Build Status](https://travis-ci.org/wheresrhys/fetch-mock.svg?branch=master)](https://travis-ci.org/wheresrhys/fetch-mock) | ||
* `sendAsJson`: This property determines whether or not the request body should be JSON.stringified before being sent (defaults to true). | ||
* `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns any of the responses listed above | ||
* `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns any of the responses listed above (or a `Promise` for any of them) | ||
@@ -146,3 +146,4 @@ #### `restore()` | ||
* 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 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. | ||
* When using karma-webpack it's best not to use the `webpack.ProvidePlugin` for this. Instead just add `node_modules/whatwg-fetch/fetch.js` to your list of files to include, or require it directly into your tests before requiring fetch-mock. | ||
@@ -149,0 +150,0 @@ ## V4 changelog |
@@ -19,2 +19,6 @@ 'use strict'; | ||
// It seems odd to call this in here even though it's already called within fetchMock | ||
// It's to handle the fact that because we want to support making it very easy to add a | ||
// delay to any sort of response (including responses which are defined with a function) | ||
// while also allowing function responses to return a Promise for a response config. | ||
if (typeof responseConfig === 'function') { | ||
@@ -114,7 +118,7 @@ responseConfig = responseConfig(url, fetchOpts); | ||
const expectedMethod = route.method && route.method.toLowerCase(); | ||
const expectedMethod = route.method && route.method.toLowerCase(); | ||
function matchMethod (method) { | ||
return !expectedMethod || expectedMethod === (method ? method.toLowerCase() : 'get'); | ||
}; | ||
function matchMethod (method) { | ||
return !expectedMethod || expectedMethod === (method ? method.toLowerCase() : 'get'); | ||
}; | ||
@@ -239,4 +243,10 @@ let matchUrl; | ||
const response = this.router(url, opts); | ||
let response = this.router(url, opts); | ||
if (response) { | ||
if (typeof response === 'function') { | ||
response = response (url, opts); | ||
} | ||
if (response instanceof Promise) { | ||
@@ -243,0 +253,0 @@ return response.then(response => mockResponse(url, response, opts)) |
@@ -43,1 +43,20 @@ 'use strict'; | ||
describe('no real fetch', function () { | ||
it('should cope when there is no global fetch defined', function () { | ||
const fetchCache = window.fetch; | ||
delete window.fetch; | ||
const realFetchCache = fetchMock.realFetch; | ||
delete fetchMock.realFetch; | ||
fetchMock.mock(/a/, 200); | ||
expect(function () { | ||
fetch('http://www.example.com'); | ||
}).not.to.throw(); | ||
expect(function () { | ||
fetchMock.calls(); | ||
}).not.to.throw(); | ||
fetchMock.restore(); | ||
fetchMock.realFetch = realFetchCache; | ||
window.fetch = fetchCache; | ||
}); | ||
}); |
@@ -16,3 +16,3 @@ 'use strict'; | ||
require('./spec')(fetchMock, GLOBAL, require('node-fetch').Request); | ||
require('./spec')(fetchMock, global, require('node-fetch').Request); | ||
@@ -22,8 +22,11 @@ describe('new non-global use', function () { | ||
before(function () { | ||
try { | ||
fetchMock.restore(); | ||
} catch (e) {} | ||
delete global.fetch; | ||
delete fetchMock.realFetch; | ||
}); | ||
it('stubs non global fetch if function passed in', function () { | ||
fetchMock.useNonGlobalFetch(dummyFetch); | ||
@@ -30,0 +33,0 @@ expect(fetchMock.realFetch).to.equal(dummyFetch); |
141
test/spec.js
@@ -191,5 +191,5 @@ 'use strict'; | ||
describe('unmatched routes', function () { | ||
it('record history of unmatched routes', function (done) { | ||
it('record history of unmatched routes', function () { | ||
fetchMock.mock(dummyRoute); | ||
Promise.all([ | ||
return Promise.all([ | ||
fetch('http://1', {method: 'GET'}), | ||
@@ -204,3 +204,2 @@ fetch('http://2', {method: 'POST'}) | ||
expect(unmatchedCalls[1]).to.eql(['http://2', {method: 'POST'}]); | ||
done(); | ||
}) | ||
@@ -210,5 +209,5 @@ | ||
it('configure to send good responses', function (done) { | ||
it('configure to send good responses', function () { | ||
fetchMock.mock({routes: dummyRoute, greed: 'good'}); | ||
fetch('http://1') | ||
return fetch('http://1') | ||
.then(function (res) { | ||
@@ -218,5 +217,4 @@ expect(fetchMock.called()).to.be.false; | ||
expect(res.status).to.equal(200); | ||
res.text().then(function (text) { | ||
return res.text().then(function (text) { | ||
expect(text).to.equal('unmocked url: http://1'); | ||
done(); | ||
}); | ||
@@ -226,15 +224,15 @@ }); | ||
it('configure to send bad responses', function (done) { | ||
it('configure to send bad responses', function () { | ||
fetchMock.mock({routes: dummyRoute, greed: 'bad'}); | ||
fetch('http://1') | ||
.catch(function (res) { | ||
return fetch('http://1') | ||
.then(function () {return Promise.reject('Expected fetch to fail')}, | ||
function (res) { | ||
expect(fetchMock.called()).to.be.false; | ||
expect(res).to.equal('unmocked url: http://1'); | ||
done(); | ||
}); | ||
}); | ||
it('configure to pass through to native fetch', function (done) { | ||
it('configure to pass through to native fetch', function () { | ||
fetchMock.mock({routes: dummyRoute, greed: 'none'}); | ||
fetch('http://1') | ||
return fetch('http://1') | ||
.then(function () { | ||
@@ -245,3 +243,2 @@ expect(fetchMock.called()).to.be.false; | ||
expect(fetchCalls[0].length).to.equal(2); | ||
done(); | ||
}); | ||
@@ -254,3 +251,3 @@ | ||
describe('route matching', function () { | ||
it('match exact strings', function (done) { | ||
it('match exact strings', function () { | ||
fetchMock.mock({ | ||
@@ -263,3 +260,3 @@ routes: { | ||
}); | ||
Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.thereabouts')]) | ||
return Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.thereabouts')]) | ||
.then(function () { | ||
@@ -271,7 +268,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls().unmatched.length).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('match when relative url', function (done) { | ||
it('match when relative url', function () { | ||
fetchMock.mock({ | ||
@@ -285,3 +281,3 @@ routes: { | ||
}); | ||
fetch('/it.at.there/', {method: 'POST'}) | ||
return fetch('/it.at.there/', {method: 'POST'}) | ||
.then(function () { | ||
@@ -292,7 +288,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls('route').length).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('match when Request instance', function (done) { | ||
it('match when Request instance', function () { | ||
fetchMock.mock({ | ||
@@ -306,3 +301,3 @@ routes: { | ||
}); | ||
fetch(new Request('http://it.at.there/', {method: 'POST'})) | ||
return fetch(new Request('http://it.at.there/', {method: 'POST'})) | ||
.then(function () { | ||
@@ -313,7 +308,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls('route').length).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('match strings starting with a string', function (done) { | ||
it('match strings starting with a string', function () { | ||
fetchMock.mock({ | ||
@@ -326,3 +320,3 @@ routes: { | ||
}); | ||
Promise.all([ | ||
return Promise.all([ | ||
fetch('http://it.at.there'), | ||
@@ -338,7 +332,6 @@ fetch('http://it.at.thereabouts'), | ||
expect(fetchMock.calls().unmatched.length).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('match regular expressions', function (done) { | ||
it('match regular expressions', function () { | ||
fetchMock.mock({ | ||
@@ -351,3 +344,3 @@ routes: { | ||
}); | ||
Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.there/12345'), fetch('http://it.at.there/abcde')]) | ||
return Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.there/12345'), fetch('http://it.at.there/abcde')]) | ||
.then(function () { | ||
@@ -359,7 +352,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls().unmatched.length).to.equal(2); | ||
done(); | ||
}); | ||
}); | ||
it('match using custom functions', function (done) { | ||
it('match using custom functions', function () { | ||
fetchMock.mock({ | ||
@@ -374,3 +366,3 @@ routes: { | ||
}); | ||
Promise.all([ | ||
return Promise.all([ | ||
fetch('http://it.at.there/logged-in', {headers:{authorized: true}}), | ||
@@ -386,7 +378,6 @@ fetch('http://it.at.there/12345', {headers:{authorized: true}}), | ||
expect(fetchMock.calls().unmatched.length).to.equal(2); | ||
done(); | ||
}); | ||
}); | ||
it('match method', function (done) { | ||
it('match method', function () { | ||
fetchMock.mock({ | ||
@@ -405,3 +396,3 @@ routes: [{ | ||
}); | ||
Promise.all([fetch('http://it.at.here/', {method: 'put'}), fetch('http://it.at.here/'), fetch('http://it.at.here/', {method: 'GET'}), fetch('http://it.at.here/', {method: 'delete'})]) | ||
return Promise.all([fetch('http://it.at.here/', {method: 'put'}), fetch('http://it.at.here/'), fetch('http://it.at.here/', {method: 'GET'}), fetch('http://it.at.here/', {method: 'delete'})]) | ||
.then(function () { | ||
@@ -415,7 +406,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls().unmatched.length).to.equal(1); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); | ||
it('match multiple routes', function (done) { | ||
it('match multiple routes', function () { | ||
fetchMock.mock({ | ||
@@ -432,3 +422,3 @@ routes: [{ | ||
}); | ||
Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.here/'), fetch('http://it.at.nowhere')]) | ||
return Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.here/'), fetch('http://it.at.nowhere')]) | ||
.then(function () { | ||
@@ -442,7 +432,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls().unmatched.length).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('match first compatible route when many routes match', function (done) { | ||
it('match first compatible route when many routes match', function () { | ||
fetchMock.mock({ | ||
@@ -459,3 +448,3 @@ routes: [{ | ||
}); | ||
Promise.all([fetch('http://it.at.there/')]) | ||
return Promise.all([fetch('http://it.at.there/')]) | ||
.then(function () { | ||
@@ -467,7 +456,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls('route2').length).to.equal(0); | ||
done(); | ||
}); | ||
}); | ||
it('record history of calls to matched routes', function (done) { | ||
it('record history of calls to matched routes', function () { | ||
fetchMock.mock({ | ||
@@ -480,3 +468,3 @@ routes: { | ||
}); | ||
Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.thereabouts', {headers: {head: 'val'}})]) | ||
return Promise.all([fetch('http://it.at.there/'), fetch('http://it.at.thereabouts', {headers: {head: 'val'}})]) | ||
.then(function () { | ||
@@ -488,7 +476,6 @@ expect(fetchMock.called()).to.be.true; | ||
expect(fetchMock.calls('route')[1]).to.eql(['http://it.at.thereabouts', {headers: {head: 'val'}}]); | ||
done(); | ||
}); | ||
}); | ||
it('have helpers to retrieve paramaters pf last call', function (done) { | ||
it('have helpers to retrieve paramaters pf last call', function () { | ||
fetchMock.mock({ | ||
@@ -507,3 +494,3 @@ routes: { | ||
}).to.not.throw; | ||
Promise.all([ | ||
return Promise.all([ | ||
fetch('http://it.at.there/first', {method: 'DELETE'}), | ||
@@ -517,3 +504,2 @@ fetch('http://it.at.there/second', {method: 'GET'}) | ||
expect(fetchMock.lastOptions()).to.deep.equal({method: 'GET'}); | ||
done(); | ||
}); | ||
@@ -523,3 +509,3 @@ | ||
it('be possible to reset call history', function (done) { | ||
it('be possible to reset call history', function () { | ||
fetchMock.mock({ | ||
@@ -532,3 +518,3 @@ routes: { | ||
}); | ||
fetch('http://it.at.there/') | ||
return fetch('http://it.at.there/') | ||
.then(function () { | ||
@@ -540,7 +526,6 @@ fetchMock.reset(); | ||
expect(fetchMock.calls().matched.length).to.equal(0); | ||
done(); | ||
}); | ||
}); | ||
it('restoring clears call history', function (done) { | ||
it('restoring clears call history', function () { | ||
fetchMock.mock({ | ||
@@ -553,3 +538,3 @@ routes: { | ||
}); | ||
fetch('http://it.at.there/') | ||
return fetch('http://it.at.there/') | ||
.then(function () { | ||
@@ -561,3 +546,2 @@ fetchMock.restore(); | ||
expect(fetchMock.calls().matched.length).to.equal(0); | ||
done(); | ||
}); | ||
@@ -570,3 +554,3 @@ }); | ||
it('respond with a status', function (done) { | ||
it('respond with a status', function () { | ||
fetchMock.mock({ | ||
@@ -579,7 +563,6 @@ routes: { | ||
}); | ||
fetch('http://it.at.there/') | ||
return fetch('http://it.at.there/') | ||
.then(function (res) { | ||
expect(res.status).to.equal(300); | ||
expect(res.statusText).to.equal('Multiple Choices'); | ||
done(); | ||
}); | ||
@@ -641,3 +624,3 @@ }); | ||
it('respond with a complex response, including headers', function (done) { | ||
it('respond with a complex response, including headers', function () { | ||
fetchMock.mock({ | ||
@@ -656,3 +639,3 @@ routes: { | ||
}); | ||
fetch('http://it.at.there/') | ||
return fetch('http://it.at.there/') | ||
.then(function (res) { | ||
@@ -663,3 +646,2 @@ expect(res.status).to.equal(202); | ||
expect(json).to.eql({an: 'object'}); | ||
done(); | ||
}); | ||
@@ -669,3 +651,3 @@ }); | ||
it('imitate a failed request', function (done) { | ||
it('imitate a failed request', function () { | ||
fetchMock.mock({ | ||
@@ -680,10 +662,10 @@ routes: { | ||
}); | ||
fetch('http://it.at.there/') | ||
.catch(function (err) { | ||
return fetch('http://it.at.there/') | ||
.then(function () {return Promise.reject('Expected fetch to fail')}, | ||
function (err) { | ||
expect(err).to.equal('Oh no'); | ||
done(); | ||
}); | ||
}); | ||
it('construct a response based on the request', function (done) { | ||
it('construct a response based on the request', function () { | ||
fetchMock.mock({ | ||
@@ -698,3 +680,3 @@ routes: { | ||
}); | ||
fetch('http://it.at.there/', {headers: {header: 'val'}}) | ||
return fetch('http://it.at.there/', {headers: {header: 'val'}}) | ||
.then(function (res) { | ||
@@ -704,3 +686,2 @@ expect(res.status).to.equal(200); | ||
expect(text).to.equal('http://it.at.there/val'); | ||
done(); | ||
}); | ||
@@ -710,2 +691,21 @@ }); | ||
it('construct a promised response based on the request', function () { | ||
fetchMock.mock({ | ||
routes: { | ||
name: 'route', | ||
matcher: 'http://it.at.there/', | ||
response: function (url, opts) { | ||
return Promise.resolve(url + opts.headers.header); | ||
} | ||
} | ||
}); | ||
return fetch('http://it.at.there/', {headers: {header: 'val'}}) | ||
.then(function (res) { | ||
expect(res.status).to.equal(200); | ||
return res.text().then(function (text) { | ||
expect(text).to.equal('http://it.at.there/val'); | ||
}); | ||
}); | ||
}); | ||
it('respond with a promise of a response', function (done) { | ||
@@ -738,5 +738,8 @@ let resolve; | ||
}); | ||
it('respond with a promise of a complex response', function (done) { | ||
it ('respond with a promise of a complex response', function (done) { | ||
let resolve; | ||
const promise = new Promise(res => {resolve = res}) | ||
fetchMock.mock({ | ||
@@ -752,2 +755,3 @@ routes: { | ||
const stub = sinon.spy(res => res); | ||
fetch('http://it.at.there/', {headers: {header: 'val'}}) | ||
@@ -773,4 +777,3 @@ .then(stub) | ||
}); | ||
}); | ||
} |
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
84277
2250
170
67