From 056f96c935f8f5bf4142ecfa044388da2ce5d3e0 Mon Sep 17 00:00:00 2001 From: Robert Stewart Date: Tue, 18 Aug 2015 10:29:02 -0600 Subject: [PATCH] Throw exception on multiple calls to jasmine.Ajax.uninstall. Running multiple tests under Karma showed some race condition behavior. --- spec/integration/mock-ajax-spec.js | 14 ++++++++++++++ src/mockAjax.js | 3 +++ 2 files changed, 17 insertions(+) diff --git a/spec/integration/mock-ajax-spec.js b/spec/integration/mock-ajax-spec.js index ad743da..66982a7 100644 --- a/spec/integration/mock-ajax-spec.js +++ b/spec/integration/mock-ajax-spec.js @@ -26,6 +26,20 @@ describe("mockAjax", function() { expect(sequentialInstalls).not.toThrow(); }); + it("does throw an error if uninstalled without a current install", function() { + var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'), + fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest }, + mockAjax = new window.MockAjax(fakeGlobal); + + function sequentialUninstalls() { + mockAjax.install(); + mockAjax.uninstall(); + mockAjax.uninstall(); + } + + expect(sequentialUninstalls).toThrow(); + }); + it("does not replace XMLHttpRequest until it is installed", function() { var fakeXmlHttpRequest = jasmine.createSpy('fakeXmlHttpRequest'), fakeGlobal = { XMLHttpRequest: fakeXmlHttpRequest }, diff --git a/src/mockAjax.js b/src/mockAjax.js index abb1879..35dda06 100644 --- a/src/mockAjax.js +++ b/src/mockAjax.js @@ -15,6 +15,9 @@ getJasmineRequireObj().MockAjax = function($ajax) { }; this.uninstall = function() { + if (global.XMLHttpRequest !== mockAjaxFunction) { + throw "MockAjax not installed. Race conditions between running tests may be uninstalling MockAjax early."; + } global.XMLHttpRequest = realAjaxFunction; this.stubs.reset();