diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 8b449d97..fb91318e 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -4117,7 +4117,17 @@ getJasmineRequireObj().GlobalErrors = function(j$) { handlers.push(listener); }; - this.popListener = function popListener() { + this.popListener = function popListener(listener) { + if (!listener) { + throw new Error('popListener expects a listener'); + } + + if (listener !== handlers[handlers.length - 1]) { + throw new Error( + 'popListener was passed a different listener than the current one' + ); + } + handlers.pop(); }; } diff --git a/spec/core/GlobalErrorsSpec.js b/spec/core/GlobalErrorsSpec.js index 3effa537..16657a7f 100644 --- a/spec/core/GlobalErrorsSpec.js +++ b/spec/core/GlobalErrorsSpec.js @@ -58,7 +58,7 @@ describe('GlobalErrors', function() { errors.pushListener(handler1); errors.pushListener(handler2); - errors.popListener(); + errors.popListener(handler2); fakeGlobal.onerror('foo'); @@ -66,6 +66,23 @@ describe('GlobalErrors', function() { expect(handler2).not.toHaveBeenCalled(); }); + it('throws when no listener is passed to #popListener', function() { + var errors = new jasmineUnderTest.GlobalErrors({}); + expect(function() { + errors.popListener(); + }).toThrowError('popListener expects a listener'); + }); + + it('throws when the argument to #popListener is not the current listener', function() { + var errors = new jasmineUnderTest.GlobalErrors({}); + errors.pushListener(function() {}); + expect(function() { + errors.popListener(function() {}); + }).toThrowError( + 'popListener was passed a different listener than the current one' + ); + }); + it('uninstalls itself, putting back a previous callback', function() { var originalCallback = jasmine.createSpy('error'), fakeGlobal = { onerror: originalCallback }, diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index cf1e997d..b51a7297 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -974,7 +974,6 @@ describe("Env integration", function() { env.execute(null, function() { expect(delayedFunctionForMockClock).toHaveBeenCalled(); expect(globalSetTimeout).toHaveBeenCalledWith(delayedFunctionForGlobalClock, 100); - done(); }); }); diff --git a/src/core/GlobalErrors.js b/src/core/GlobalErrors.js index 282eb262..b79976d9 100644 --- a/src/core/GlobalErrors.js +++ b/src/core/GlobalErrors.js @@ -96,7 +96,17 @@ getJasmineRequireObj().GlobalErrors = function(j$) { handlers.push(listener); }; - this.popListener = function popListener() { + this.popListener = function popListener(listener) { + if (!listener) { + throw new Error('popListener expects a listener'); + } + + if (listener !== handlers[handlers.length - 1]) { + throw new Error( + 'popListener was passed a different listener than the current one' + ); + } + handlers.pop(); }; }