diff --git a/src/lib/compile-route.js b/src/lib/compile-route.js index 8580ce56..aa4377f6 100644 --- a/src/lib/compile-route.js +++ b/src/lib/compile-route.js @@ -186,7 +186,6 @@ const generateMatcher = (route, config) => { } const limitMatcher = (route) => { - if (!route.repeat) { return; } diff --git a/src/lib/index.js b/src/lib/index.js index db8811ce..3cd878a8 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -14,7 +14,8 @@ FetchMock.config = { FetchMock.createInstance = function () { const instance = Object.create(FetchMock); - instance.routes = (this.routes || []).slice(); + instance._uncompiledRoutes = (this._uncompiledRoutes || []).slice(); + instance.routes = instance._uncompiledRoutes.map(config => instance.compileRoute(config)); instance.fallbackResponse = this.fallbackResponse || undefined; instance.config = Object.assign({}, this.config || FetchMock.config); instance._calls = {}; diff --git a/src/lib/inspecting.js b/src/lib/inspecting.js index 4995f518..fe6f5fd6 100644 --- a/src/lib/inspecting.js +++ b/src/lib/inspecting.js @@ -93,4 +93,4 @@ FetchMock.done = function (name) { .filter(bool => !bool).length === 0 }; -module.exports = FetchMock; \ No newline at end of file +module.exports = FetchMock; diff --git a/src/lib/set-up-and-tear-down.js b/src/lib/set-up-and-tear-down.js index 5d906c3b..0d938585 100644 --- a/src/lib/set-up-and-tear-down.js +++ b/src/lib/set-up-and-tear-down.js @@ -24,6 +24,7 @@ FetchMock.mock = function (matcher, response, options = {}) { const getMatcher = (route, propName) => (route2) => route[propName] === route2[propName]; FetchMock.addRoute = function (route) { + this._uncompiledRoutes.push(route); route = this.compileRoute(route); const clashes = this.routes.filter(getMatcher(route, 'name')); @@ -91,6 +92,7 @@ FetchMock.restore = function () { } this.fallbackResponse = undefined; this.routes = []; + this._uncompiledRoutes = []; this.reset(); return this; } @@ -103,4 +105,4 @@ FetchMock.reset = function () { return this; } -module.exports = FetchMock; \ No newline at end of file +module.exports = FetchMock; diff --git a/test/specs/repeat.test.js b/test/specs/repeat.test.js index 2d45a31f..da754e1b 100644 --- a/test/specs/repeat.test.js +++ b/test/specs/repeat.test.js @@ -128,6 +128,67 @@ module.exports = (fetchMock) => { console.warn.restore();//eslint-disable-line }); + describe('sandbox isolation', () => { + it('doesn\'t propagate to children of global', () => { + fm + .mock('http://it.at.there/', 200, {repeat: 1}); + + const sb1 = fm.sandbox(); + + fm.fetchHandler('http://it.at.there/'); + + expect(fm.done()).to.be.true; + expect(sb1.done()).to.be.false; + + expect(() => sb1.fetchHandler('http://it.at.there/')).not.to.throw(); + }); + + it('doesn\'t propagate to global from children', () => { + fm + .mock('http://it.at.there/', 200, {repeat: 1}); + + const sb1 = fm.sandbox(); + + sb1.fetchHandler('http://it.at.there/'); + + expect(fm.done()).to.be.false; + expect(sb1.done()).to.be.true; + + expect(() => fm.fetchHandler('http://it.at.there/')).not.to.throw(); + }); + + it('doesn\'t propagate to children of sandbox', () => { + const sb1 = fm + .sandbox() + .mock('http://it.at.there/', 200, {repeat: 1}); + + const sb2 = sb1.sandbox(); + + sb1.fetchHandler('http://it.at.there/'); + + expect(sb1.done()).to.be.true; + expect(sb2.done()).to.be.false; + + expect(() => sb2.fetchHandler('http://it.at.there/')).not.to.throw(); + }); + + it('doesn\'t propagate to sandbox from children', () => { + const sb1 = fm + .sandbox() + .mock('http://it.at.there/', 200, {repeat: 1}); + + const sb2 = sb1.sandbox(); + + sb2.fetchHandler('http://it.at.there/'); + + expect(sb1.done()).to.be.false; + expect(sb2.done()).to.be.true; + + expect(() => sb1.fetchHandler('http://it.at.there/')).not.to.throw(); + }); + }); + + describe('strict matching shorthands', () => { it('has once shorthand method', () => { sinon.stub(fm, 'mock');