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).
This commit is contained in:
Steve Gravrock
2022-08-20 10:27:44 -07:00
parent 79c6bbc189
commit f934e6d816
4 changed files with 66 additions and 56 deletions

View File

@@ -4081,21 +4081,14 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
} }
}; };
if (global.addEventListener) { global.addEventListener('unhandledrejection', browserRejectionHandler);
global.addEventListener(
'unhandledrejection',
browserRejectionHandler
);
}
this.uninstall = function uninstall() { this.uninstall = function uninstall() {
global.onerror = originalHandler; global.onerror = originalHandler;
if (global.removeEventListener) { global.removeEventListener(
global.removeEventListener( 'unhandledrejection',
'unhandledrejection', browserRejectionHandler
browserRejectionHandler );
);
}
}; };
} }
}; };

View File

@@ -1,8 +1,8 @@
describe('GlobalErrors', function() { describe('GlobalErrors', function() {
it('calls the added handler on error', function() { it('calls the added handler on error', function() {
const fakeGlobal = { onerror: null }, const fakeGlobal = minimalBrowserGlobal();
handler = jasmine.createSpy('errorHandler'), const handler = jasmine.createSpy('errorHandler');
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install(); errors.install();
errors.pushListener(handler); errors.pushListener(handler);
@@ -13,10 +13,10 @@ describe('GlobalErrors', function() {
}); });
it('enables external interception of error by overriding global.onerror', function() { it('enables external interception of error by overriding global.onerror', function() {
const fakeGlobal = { onerror: null }, const fakeGlobal = minimalBrowserGlobal();
handler = jasmine.createSpy('errorHandler'), const handler = jasmine.createSpy('errorHandler');
hijackHandler = jasmine.createSpy('hijackErrorHandler'), const hijackHandler = jasmine.createSpy('hijackErrorHandler');
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install(); errors.install();
errors.pushListener(handler); errors.pushListener(handler);
@@ -30,10 +30,10 @@ describe('GlobalErrors', function() {
}); });
it('calls the global error handler with all parameters', function() { it('calls the global error handler with all parameters', function() {
const fakeGlobal = { onerror: null }, const fakeGlobal = minimalBrowserGlobal();
handler = jasmine.createSpy('errorHandler'), const handler = jasmine.createSpy('errorHandler');
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal), const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
fooError = new Error('foo'); const fooError = new Error('foo');
errors.install(); errors.install();
errors.pushListener(handler); errors.pushListener(handler);
@@ -50,10 +50,10 @@ describe('GlobalErrors', function() {
}); });
it('only calls the most recent handler', function() { it('only calls the most recent handler', function() {
const fakeGlobal = { onerror: null }, const fakeGlobal = minimalBrowserGlobal();
handler1 = jasmine.createSpy('errorHandler1'), const handler1 = jasmine.createSpy('errorHandler1');
handler2 = jasmine.createSpy('errorHandler2'), const handler2 = jasmine.createSpy('errorHandler2');
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install(); errors.install();
errors.pushListener(handler1); errors.pushListener(handler1);
@@ -66,10 +66,10 @@ describe('GlobalErrors', function() {
}); });
it('calls previous handlers when one is removed', function() { it('calls previous handlers when one is removed', function() {
const fakeGlobal = { onerror: null }, const fakeGlobal = minimalBrowserGlobal();
handler1 = jasmine.createSpy('errorHandler1'), const handler1 = jasmine.createSpy('errorHandler1');
handler2 = jasmine.createSpy('errorHandler2'), const handler2 = jasmine.createSpy('errorHandler2');
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install(); errors.install();
errors.pushListener(handler1); errors.pushListener(handler1);
@@ -91,9 +91,12 @@ describe('GlobalErrors', function() {
}); });
it('uninstalls itself, putting back a previous callback', function() { it('uninstalls itself, putting back a previous callback', function() {
const originalCallback = jasmine.createSpy('error'), const originalCallback = jasmine.createSpy('error');
fakeGlobal = { onerror: originalCallback }, const fakeGlobal = {
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); ...minimalBrowserGlobal(),
onerror: originalCallback
};
const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
expect(fakeGlobal.onerror).toBe(originalCallback); expect(fakeGlobal.onerror).toBe(originalCallback);
@@ -107,9 +110,9 @@ describe('GlobalErrors', function() {
}); });
it('rethrows the original error when there is no handler', function() { it('rethrows the original error when there is no handler', function() {
const fakeGlobal = {}, const fakeGlobal = minimalBrowserGlobal();
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal), const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
originalError = new Error('nope'); const originalError = new Error('nope');
errors.install(); errors.install();
@@ -407,7 +410,7 @@ describe('GlobalErrors', function() {
describe('#setOverrideListener', function() { describe('#setOverrideListener', function() {
it('overrides the existing handlers in browsers until removed', function() { it('overrides the existing handlers in browsers until removed', function() {
const fakeGlobal = { onerror: null }; const fakeGlobal = minimalBrowserGlobal();
const handler0 = jasmine.createSpy('handler0'); const handler0 = jasmine.createSpy('handler0');
const handler1 = jasmine.createSpy('handler1'); const handler1 = jasmine.createSpy('handler1');
const overrideHandler = jasmine.createSpy('overrideHandler'); const overrideHandler = jasmine.createSpy('overrideHandler');
@@ -529,8 +532,7 @@ describe('GlobalErrors', function() {
}); });
it('throws if there is already an override handler', function() { it('throws if there is already an override handler', function() {
const fakeGlobal = { onerror: null }; const errors = new jasmineUnderTest.GlobalErrors(minimalBrowserGlobal());
const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.setOverrideListener(() => {}, () => {}); errors.setOverrideListener(() => {}, () => {});
expect(function() { expect(function() {
@@ -541,9 +543,8 @@ describe('GlobalErrors', function() {
describe('#removeOverrideListener', function() { describe('#removeOverrideListener', function() {
it("calls the handler's onRemove callback", function() { it("calls the handler's onRemove callback", function() {
const fakeGlobal = { onerror: null };
const onRemove = jasmine.createSpy('onRemove'); const onRemove = jasmine.createSpy('onRemove');
const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); const errors = new jasmineUnderTest.GlobalErrors(minimalBrowserGlobal());
errors.setOverrideListener(() => {}, onRemove); errors.setOverrideListener(() => {}, onRemove);
errors.removeOverrideListener(); errors.removeOverrideListener();
@@ -552,10 +553,17 @@ describe('GlobalErrors', function() {
}); });
it('does not throw if there is no handler', function() { it('does not throw if there is no handler', function() {
const fakeGlobal = { onerror: null }; const errors = new jasmineUnderTest.GlobalErrors(minimalBrowserGlobal());
const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
expect(() => errors.removeOverrideListener()).not.toThrow(); expect(() => errors.removeOverrideListener()).not.toThrow();
}); });
}); });
function minimalBrowserGlobal() {
return {
addEventListener() {},
removeEventListener() {},
onerror: null
};
}
}); });

View File

@@ -431,6 +431,7 @@ describe('Env integration', function() {
describe('Handling async errors', function() { describe('Handling async errors', function() {
it('routes async errors to a running spec', async function() { it('routes async errors to a running spec', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -471,6 +472,7 @@ describe('Env integration', function() {
describe('When the running spec has reported specDone', function() { describe('When the running spec has reported specDone', function() {
it('routes async errors to an ancestor suite', async function() { it('routes async errors to an ancestor suite', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -530,6 +532,7 @@ describe('Env integration', function() {
it('routes async errors to a running suite', async function() { it('routes async errors to a running suite', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -582,6 +585,7 @@ describe('Env integration', function() {
describe('When the running suite has reported suiteDone', function() { describe('When the running suite has reported suiteDone', function() {
it('routes async errors to an ancestor suite', async function() { it('routes async errors to an ancestor suite', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -645,6 +649,7 @@ describe('Env integration', function() {
describe('When the env has started reporting jasmineDone', function() { describe('When the env has started reporting jasmineDone', function() {
it('logs the error to the console', async function() { it('logs the error to the console', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(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() { it('routes all errors that occur during stack clearing somewhere', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -2637,6 +2643,7 @@ describe('Env integration', function() {
it('reports errors that occur during loading', async function() { it('reports errors that occur during loading', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(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() { it('does not install a global error handler during loading', async function() {
const originalOnerror = jasmine.createSpy('original onerror'); const originalOnerror = jasmine.createSpy('original onerror');
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -2888,6 +2896,7 @@ describe('Env integration', function() {
describe('When there are load errors', function() { describe('When there are load errors', function() {
it('is "failed"', async function() { it('is "failed"', async function() {
const global = { const global = {
...browserEventMethods(),
setTimeout: function(fn, delay) { setTimeout: function(fn, delay) {
return setTimeout(fn, delay); return setTimeout(fn, delay);
}, },
@@ -3933,4 +3942,11 @@ describe('Env integration', function() {
); );
}); });
}); });
function browserEventMethods() {
return {
addEventListener() {},
removeEventListener() {}
};
}
}); });

View File

@@ -109,21 +109,14 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
} }
}; };
if (global.addEventListener) { global.addEventListener('unhandledrejection', browserRejectionHandler);
global.addEventListener(
'unhandledrejection',
browserRejectionHandler
);
}
this.uninstall = function uninstall() { this.uninstall = function uninstall() {
global.onerror = originalHandler; global.onerror = originalHandler;
if (global.removeEventListener) { global.removeEventListener(
global.removeEventListener( 'unhandledrejection',
'unhandledrejection', browserRejectionHandler
browserRejectionHandler );
);
}
}; };
} }
}; };