diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 2bbc610c..0506c7c7 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -114,7 +114,19 @@ var getJasmineRequireObj = (function(jasmineGlobal) { j$.NullDiffBuilder = jRequire.NullDiffBuilder(j$); j$.ObjectPath = jRequire.ObjectPath(j$); j$.MismatchTree = jRequire.MismatchTree(j$); - j$.GlobalErrors = jRequire.GlobalErrors(j$); + + // zone.js tries to monkey patch GlobalErrors in a way that is either a + // no-op or causes Jasmine to crash, depending on whether it's done before + // or after env creation. Prevent that. + const GlobalErrors = jRequire.GlobalErrors(j$); + Object.defineProperty(j$, 'GlobalErrors', { + enumerable: true, + configurable: false, + get() { + return GlobalErrors; + }, + set() {} + }); j$.Truthy = jRequire.Truthy(j$); j$.Falsy = jRequire.Falsy(j$); @@ -1195,6 +1207,7 @@ getJasmineRequireObj().Env = function(j$) { options = options || {}; const self = this; + const GlobalErrors = options.GlobalErrors || j$.GlobalErrors; const global = options.global || j$.getGlobal(); const realSetTimeout = global.setTimeout; @@ -1208,7 +1221,7 @@ getJasmineRequireObj().Env = function(j$) { new j$.MockDate(global) ); - const globalErrors = new j$.GlobalErrors( + const globalErrors = new GlobalErrors( undefined, // Configuration is late-bound because GlobalErrors needs to be constructed // before it's set to detect load-time errors in browsers diff --git a/spec/core/EnvSpec.js b/spec/core/EnvSpec.js index a3753f77..db258a00 100644 --- a/spec/core/EnvSpec.js +++ b/spec/core/EnvSpec.js @@ -625,9 +625,12 @@ describe('Env', function() { 'popListener', 'removeOverrideListener' ]); - spyOn(jasmineUnderTest, 'GlobalErrors').and.returnValue(globalErrors); env.cleanup_(); - env = new jasmineUnderTest.Env(); + env = new jasmineUnderTest.Env({ + GlobalErrors: function() { + return globalErrors; + } + }); expect(globalErrors.install).toHaveBeenCalled(); }); }); @@ -641,9 +644,13 @@ describe('Env', function() { 'popListener', 'removeOverrideListener' ]); - spyOn(jasmineUnderTest, 'GlobalErrors').and.returnValue(globalErrors); env.cleanup_(); - env = new jasmineUnderTest.Env({ suppressLoadErrors: true }); + env = new jasmineUnderTest.Env({ + suppressLoadErrors: true, + GlobalErrors: function() { + return globalErrors; + } + }); expect(globalErrors.install).not.toHaveBeenCalled(); env.execute(); expect(globalErrors.install).toHaveBeenCalled(); diff --git a/spec/core/integration/GlobalErrorHandlingSpec.js b/spec/core/integration/GlobalErrorHandlingSpec.js index 3cfe09b0..814d5ec4 100644 --- a/spec/core/integration/GlobalErrorHandlingSpec.js +++ b/spec/core/integration/GlobalErrorHandlingSpec.js @@ -88,10 +88,14 @@ describe('Global error handling (integration)', function() { const globalErrors = new jasmineUnderTest.GlobalErrors(global); const onerror = jasmine.createSpy('onerror'); globalErrors.pushListener(onerror); - spyOn(jasmineUnderTest, 'GlobalErrors').and.returnValue(globalErrors); env.cleanup_(); - env = new jasmineUnderTest.Env({ suppressLoadErrors: true }); + env = new jasmineUnderTest.Env({ + suppressLoadErrors: true, + GlobalErrors: function() { + return globalErrors; + } + }); const reporter = jasmine.createSpyObj('reporter', [ 'jasmineDone', 'suiteDone', diff --git a/src/core/Env.js b/src/core/Env.js index 7259ee8d..d8309d8e 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -11,6 +11,7 @@ getJasmineRequireObj().Env = function(j$) { options = options || {}; const self = this; + const GlobalErrors = options.GlobalErrors || j$.GlobalErrors; const global = options.global || j$.getGlobal(); const realSetTimeout = global.setTimeout; @@ -24,7 +25,7 @@ getJasmineRequireObj().Env = function(j$) { new j$.MockDate(global) ); - const globalErrors = new j$.GlobalErrors( + const globalErrors = new GlobalErrors( undefined, // Configuration is late-bound because GlobalErrors needs to be constructed // before it's set to detect load-time errors in browsers diff --git a/src/core/requireCore.js b/src/core/requireCore.js index c0f9c884..c4ade812 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -90,7 +90,19 @@ var getJasmineRequireObj = (function(jasmineGlobal) { j$.NullDiffBuilder = jRequire.NullDiffBuilder(j$); j$.ObjectPath = jRequire.ObjectPath(j$); j$.MismatchTree = jRequire.MismatchTree(j$); - j$.GlobalErrors = jRequire.GlobalErrors(j$); + + // zone.js tries to monkey patch GlobalErrors in a way that is either a + // no-op or causes Jasmine to crash, depending on whether it's done before + // or after env creation. Prevent that. + const GlobalErrors = jRequire.GlobalErrors(j$); + Object.defineProperty(j$, 'GlobalErrors', { + enumerable: true, + configurable: false, + get() { + return GlobalErrors; + }, + set() {} + }); j$.Truthy = jRequire.Truthy(j$); j$.Falsy = jRequire.Falsy(j$);