diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index eeb857f1..a8c70600 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1157,12 +1157,21 @@ getJasmineRequireObj().Env = function(j$) { // before it's set to detect load-time errors in browsers () => this.configuration() ); - const installGlobalErrors = (function() { + const { installGlobalErrors, uninstallGlobalErrors } = (function() { let installed = false; - return function() { - if (!installed) { - globalErrors.install(); - installed = true; + + return { + installGlobalErrors() { + if (!installed) { + globalErrors.install(); + installed = true; + } + }, + uninstallGlobalErrors() { + if (installed) { + globalErrors.uninstall(); + installed = false; + } } }; })(); @@ -2086,9 +2095,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.cleanup_ = function() { - if (globalErrors) { - globalErrors.uninstall(); - } + uninstallGlobalErrors(); }; } diff --git a/spec/core/integration/GlobalErrorHandlingSpec.js b/spec/core/integration/GlobalErrorHandlingSpec.js index c7367d5c..55e5ac47 100644 --- a/spec/core/integration/GlobalErrorHandlingSpec.js +++ b/spec/core/integration/GlobalErrorHandlingSpec.js @@ -1011,6 +1011,43 @@ describe('Global error handling (integration)', function() { }); }); + it('works when the suite is run multiple times', async function() { + const global = { + ...browserEventMethods(), + setTimeout: function(fn, delay) { + return setTimeout(fn, delay); + }, + clearTimeout: function(fn, delay) { + clearTimeout(fn, delay); + }, + queueMicrotask: function(fn) { + queueMicrotask(fn); + } + }; + spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global); + env.cleanup_(); + env = new jasmineUnderTest.Env(); + env.configure({ autoCleanClosures: false }); + const reporter = jasmine.createSpyObj('fakeReporter', ['specDone']); + + env.addReporter(reporter); + + env.it('fails', function(specDone) { + setTimeout(function() { + dispatchErrorEvent(global, 'error', { error: 'fail' }); + specDone(); + }); + }); + + await env.execute(); + reporter.specDone.calls.reset(); + await env.execute(); + + expect(reporter.specDone).toHaveFailedExpectationsForRunnable('fails', [ + 'fail thrown' + ]); + }); + describe('#spyOnGlobalErrorsAsync', function() { const leftInstalledMessage = 'Global error spy was not uninstalled. ' + diff --git a/src/core/Env.js b/src/core/Env.js index be7a24be..6842bbca 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -31,12 +31,21 @@ getJasmineRequireObj().Env = function(j$) { // before it's set to detect load-time errors in browsers () => this.configuration() ); - const installGlobalErrors = (function() { + const { installGlobalErrors, uninstallGlobalErrors } = (function() { let installed = false; - return function() { - if (!installed) { - globalErrors.install(); - installed = true; + + return { + installGlobalErrors() { + if (!installed) { + globalErrors.install(); + installed = true; + } + }, + uninstallGlobalErrors() { + if (installed) { + globalErrors.uninstall(); + installed = false; + } } }; })(); @@ -960,9 +969,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.cleanup_ = function() { - if (globalErrors) { - globalErrors.uninstall(); - } + uninstallGlobalErrors(); }; }