From f934e6d8167024b5adca90cb45630409f2eb1462 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Sat, 20 Aug 2022 10:27:44 -0700 Subject: [PATCH] Assume that addEventListener/removeEventListener are present in browsers Jasmine 3.0 dropped support for the last browser that didn't support the standard event handler methods (IE 9). --- lib/jasmine-core/jasmine.js | 17 +++----- spec/core/GlobalErrorsSpec.js | 72 ++++++++++++++++++-------------- spec/core/integration/EnvSpec.js | 16 +++++++ src/core/GlobalErrors.js | 17 +++----- 4 files changed, 66 insertions(+), 56 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 534da5df..166ffdb6 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -4081,21 +4081,14 @@ getJasmineRequireObj().GlobalErrors = function(j$) { } }; - if (global.addEventListener) { - global.addEventListener( - 'unhandledrejection', - browserRejectionHandler - ); - } + global.addEventListener('unhandledrejection', browserRejectionHandler); this.uninstall = function uninstall() { global.onerror = originalHandler; - if (global.removeEventListener) { - global.removeEventListener( - 'unhandledrejection', - browserRejectionHandler - ); - } + global.removeEventListener( + 'unhandledrejection', + browserRejectionHandler + ); }; } }; diff --git a/spec/core/GlobalErrorsSpec.js b/spec/core/GlobalErrorsSpec.js index 8c8c9e12..dfb714ad 100644 --- a/spec/core/GlobalErrorsSpec.js +++ b/spec/core/GlobalErrorsSpec.js @@ -1,8 +1,8 @@ describe('GlobalErrors', function() { it('calls the added handler on error', function() { - const fakeGlobal = { onerror: null }, - handler = jasmine.createSpy('errorHandler'), - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const fakeGlobal = minimalBrowserGlobal(); + const handler = jasmine.createSpy('errorHandler'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); errors.install(); errors.pushListener(handler); @@ -13,10 +13,10 @@ describe('GlobalErrors', function() { }); it('enables external interception of error by overriding global.onerror', function() { - const fakeGlobal = { onerror: null }, - handler = jasmine.createSpy('errorHandler'), - hijackHandler = jasmine.createSpy('hijackErrorHandler'), - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const fakeGlobal = minimalBrowserGlobal(); + const handler = jasmine.createSpy('errorHandler'); + const hijackHandler = jasmine.createSpy('hijackErrorHandler'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); errors.install(); errors.pushListener(handler); @@ -30,10 +30,10 @@ describe('GlobalErrors', function() { }); it('calls the global error handler with all parameters', function() { - const fakeGlobal = { onerror: null }, - handler = jasmine.createSpy('errorHandler'), - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal), - fooError = new Error('foo'); + const fakeGlobal = minimalBrowserGlobal(); + const handler = jasmine.createSpy('errorHandler'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const fooError = new Error('foo'); errors.install(); errors.pushListener(handler); @@ -50,10 +50,10 @@ describe('GlobalErrors', function() { }); it('only calls the most recent handler', function() { - const fakeGlobal = { onerror: null }, - handler1 = jasmine.createSpy('errorHandler1'), - handler2 = jasmine.createSpy('errorHandler2'), - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const fakeGlobal = minimalBrowserGlobal(); + const handler1 = jasmine.createSpy('errorHandler1'); + const handler2 = jasmine.createSpy('errorHandler2'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); errors.install(); errors.pushListener(handler1); @@ -66,10 +66,10 @@ describe('GlobalErrors', function() { }); it('calls previous handlers when one is removed', function() { - const fakeGlobal = { onerror: null }, - handler1 = jasmine.createSpy('errorHandler1'), - handler2 = jasmine.createSpy('errorHandler2'), - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const fakeGlobal = minimalBrowserGlobal(); + const handler1 = jasmine.createSpy('errorHandler1'); + const handler2 = jasmine.createSpy('errorHandler2'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); errors.install(); errors.pushListener(handler1); @@ -91,9 +91,12 @@ describe('GlobalErrors', function() { }); it('uninstalls itself, putting back a previous callback', function() { - const originalCallback = jasmine.createSpy('error'), - fakeGlobal = { onerror: originalCallback }, - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const originalCallback = jasmine.createSpy('error'); + const fakeGlobal = { + ...minimalBrowserGlobal(), + onerror: originalCallback + }; + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); expect(fakeGlobal.onerror).toBe(originalCallback); @@ -107,9 +110,9 @@ describe('GlobalErrors', function() { }); it('rethrows the original error when there is no handler', function() { - const fakeGlobal = {}, - errors = new jasmineUnderTest.GlobalErrors(fakeGlobal), - originalError = new Error('nope'); + const fakeGlobal = minimalBrowserGlobal(); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const originalError = new Error('nope'); errors.install(); @@ -407,7 +410,7 @@ describe('GlobalErrors', function() { describe('#setOverrideListener', function() { it('overrides the existing handlers in browsers until removed', function() { - const fakeGlobal = { onerror: null }; + const fakeGlobal = minimalBrowserGlobal(); const handler0 = jasmine.createSpy('handler0'); const handler1 = jasmine.createSpy('handler1'); const overrideHandler = jasmine.createSpy('overrideHandler'); @@ -529,8 +532,7 @@ describe('GlobalErrors', function() { }); it('throws if there is already an override handler', function() { - const fakeGlobal = { onerror: null }; - const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const errors = new jasmineUnderTest.GlobalErrors(minimalBrowserGlobal()); errors.setOverrideListener(() => {}, () => {}); expect(function() { @@ -541,9 +543,8 @@ describe('GlobalErrors', function() { describe('#removeOverrideListener', function() { it("calls the handler's onRemove callback", function() { - const fakeGlobal = { onerror: null }; const onRemove = jasmine.createSpy('onRemove'); - const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const errors = new jasmineUnderTest.GlobalErrors(minimalBrowserGlobal()); errors.setOverrideListener(() => {}, onRemove); errors.removeOverrideListener(); @@ -552,10 +553,17 @@ describe('GlobalErrors', function() { }); it('does not throw if there is no handler', function() { - const fakeGlobal = { onerror: null }; - const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + const errors = new jasmineUnderTest.GlobalErrors(minimalBrowserGlobal()); expect(() => errors.removeOverrideListener()).not.toThrow(); }); }); + + function minimalBrowserGlobal() { + return { + addEventListener() {}, + removeEventListener() {}, + onerror: null + }; + } }); diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 92f27ad4..b61322d2 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -431,6 +431,7 @@ describe('Env integration', function() { describe('Handling async errors', function() { it('routes async errors to a running spec', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -471,6 +472,7 @@ describe('Env integration', function() { describe('When the running spec has reported specDone', function() { it('routes async errors to an ancestor suite', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -530,6 +532,7 @@ describe('Env integration', function() { it('routes async errors to a running suite', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -582,6 +585,7 @@ describe('Env integration', function() { describe('When the running suite has reported suiteDone', function() { it('routes async errors to an ancestor suite', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -645,6 +649,7 @@ describe('Env integration', function() { describe('When the env has started reporting jasmineDone', function() { it('logs the error to the console', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -687,6 +692,7 @@ describe('Env integration', function() { it('routes all errors that occur during stack clearing somewhere', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -2637,6 +2643,7 @@ describe('Env integration', function() { it('reports errors that occur during loading', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -2695,6 +2702,7 @@ describe('Env integration', function() { it('does not install a global error handler during loading', async function() { const originalOnerror = jasmine.createSpy('original onerror'); const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -2888,6 +2896,7 @@ describe('Env integration', function() { describe('When there are load errors', function() { it('is "failed"', async function() { const global = { + ...browserEventMethods(), setTimeout: function(fn, delay) { return setTimeout(fn, delay); }, @@ -3933,4 +3942,11 @@ describe('Env integration', function() { ); }); }); + + function browserEventMethods() { + return { + addEventListener() {}, + removeEventListener() {} + }; + } }); diff --git a/src/core/GlobalErrors.js b/src/core/GlobalErrors.js index bd121681..11b9697f 100644 --- a/src/core/GlobalErrors.js +++ b/src/core/GlobalErrors.js @@ -109,21 +109,14 @@ getJasmineRequireObj().GlobalErrors = function(j$) { } }; - if (global.addEventListener) { - global.addEventListener( - 'unhandledrejection', - browserRejectionHandler - ); - } + global.addEventListener('unhandledrejection', browserRejectionHandler); this.uninstall = function uninstall() { global.onerror = originalHandler; - if (global.removeEventListener) { - global.removeEventListener( - 'unhandledrejection', - browserRejectionHandler - ); - } + global.removeEventListener( + 'unhandledrejection', + browserRejectionHandler + ); }; } };