diff --git a/lib/jasmine-core/boot/node_boot.js b/lib/jasmine-core/boot/node_boot.js index 304bcd12..89ee49b1 100644 --- a/lib/jasmine-core/boot/node_boot.js +++ b/lib/jasmine-core/boot/node_boot.js @@ -1,7 +1,7 @@ module.exports = function(jasmineRequire) { var jasmine = jasmineRequire.core(jasmineRequire); - var env = jasmine.getEnv(); + var env = jasmine.getEnv({suppressLoadErrors: true}); var jasmineInterface = jasmineRequire.interface(jasmine, env); diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 3fdbeacc..aaa8ec7a 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -794,17 +794,29 @@ getJasmineRequireObj().Env = function(j$) { return currentSpec || currentSuite(); }; - var globalErrors = new j$.GlobalErrors(); - globalErrors.install(); - globalErrors.pushListener(function(message, filename, lineno) { - topSuite.result.failedExpectations.push({ - passed: false, - globalErrorType: 'load', - message: message, - filename: filename, - lineno: lineno + var globalErrors = null; + + var installGlobalErrors = function() { + if (globalErrors) { + return; + } + + globalErrors = new j$.GlobalErrors(); + globalErrors.install(); + }; + + if (!options.suppressLoadErrors) { + installGlobalErrors(); + globalErrors.pushListener(function(message, filename, lineno) { + topSuite.result.failedExpectations.push({ + passed: false, + globalErrorType: 'load', + message: message, + filename: filename, + lineno: lineno + }); }); - }); + } this.specFilter = function() { return true; @@ -949,13 +961,6 @@ getJasmineRequireObj().Env = function(j$) { return seed; }; - this.suppressLoadErrors = function() { - if (handlingLoadErrors) { - globalErrors.popListener(); - } - handlingLoadErrors = false; - }; - this.deprecated = function(deprecation) { var runnable = currentRunnable() || topSuite; runnable.addDeprecationWarning(deprecation); @@ -1065,7 +1070,7 @@ getJasmineRequireObj().Env = function(j$) { this.execute = function(runnablesToRun) { var self = this; - this.suppressLoadErrors(); + installGlobalErrors(); if(!runnablesToRun) { if (focusedRunnables.length) { diff --git a/lib/jasmine-core/node_boot.js b/lib/jasmine-core/node_boot.js index 2b2d7adf..234b0a7f 100644 --- a/lib/jasmine-core/node_boot.js +++ b/lib/jasmine-core/node_boot.js @@ -23,7 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. module.exports = function(jasmineRequire) { var jasmine = jasmineRequire.core(jasmineRequire); - var env = jasmine.getEnv(); + var env = jasmine.getEnv({suppressLoadErrors: true}); var jasmineInterface = jasmineRequire.interface(jasmine, env); diff --git a/spec/core/EnvSpec.js b/spec/core/EnvSpec.js index bf239707..5c9118c8 100644 --- a/spec/core/EnvSpec.js +++ b/spec/core/EnvSpec.js @@ -197,4 +197,24 @@ describe("Env", function() { }).not.toThrow(); }); }); + + describe('when not constructed with suppressLoadErrors: true', function() { + it('installs a global error handler on construction', function() { + var globalErrors = jasmine.createSpyObj('globalErrors', ['install', 'pushListener', 'popListener']); + spyOn(jasmineUnderTest, 'GlobalErrors').and.returnValue(globalErrors); + new jasmineUnderTest.Env(); + expect(globalErrors.install).toHaveBeenCalled(); + }); + }); + + describe('when constructed with suppressLoadErrors: true', function() { + it('does not install a global error handler until execute is called', function() { + var globalErrors = jasmine.createSpyObj('globalErrors', ['install', 'pushListener', 'popListener']); + spyOn(jasmineUnderTest, 'GlobalErrors').and.returnValue(globalErrors); + env = new jasmineUnderTest.Env({suppressLoadErrors: true}); + expect(globalErrors.install).not.toHaveBeenCalled(); + env.execute(); + expect(globalErrors.install).toHaveBeenCalled(); + }); + }); }); diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 93baf7ed..0fe392b4 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -2060,11 +2060,13 @@ describe("Env integration", function() { env.execute(); }); - describe('If suppressLoadErrors was called', function() { - it('does not report or handle errors that occur during loading', function(done) { + describe('If suppressLoadErrors: true was passed', function() { + it('does not install a global error handler during loading', function(done) { + var originalOnerror = jasmine.createSpy('original onerror') var global = { setTimeout: function(fn, delay) { setTimeout(fn, delay) }, - clearTimeout: function(fn, delay) { clearTimeout(fn, delay) } + clearTimeout: function(fn, delay) { clearTimeout(fn, delay) }, + onerror: originalOnerror }; spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global); var globalErrors = new jasmineUnderTest.GlobalErrors(global); @@ -2072,17 +2074,16 @@ describe("Env integration", function() { globalErrors.pushListener(onerror); spyOn(jasmineUnderTest, 'GlobalErrors').and.returnValue(globalErrors); - var env = new jasmineUnderTest.Env(), + var env = new jasmineUnderTest.Env({suppressLoadErrors: true}); reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']); reporter.jasmineDone.and.callFake(function(e) { expect(e.failedExpectations).toEqual([]); - expect(onerror).toHaveBeenCalledWith('Uncaught Error: ENOCHEESE'); + expect(originalOnerror).toHaveBeenCalledWith('Uncaught Error: ENOCHEESE'); done(); }); env.addReporter(reporter); - env.suppressLoadErrors(true); global.onerror('Uncaught Error: ENOCHEESE'); env.execute(); diff --git a/src/core/Env.js b/src/core/Env.js index 011cfe4e..2e7f0729 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -38,17 +38,29 @@ getJasmineRequireObj().Env = function(j$) { return currentSpec || currentSuite(); }; - var globalErrors = new j$.GlobalErrors(); - globalErrors.install(); - globalErrors.pushListener(function(message, filename, lineno) { - topSuite.result.failedExpectations.push({ - passed: false, - globalErrorType: 'load', - message: message, - filename: filename, - lineno: lineno + var globalErrors = null; + + var installGlobalErrors = function() { + if (globalErrors) { + return; + } + + globalErrors = new j$.GlobalErrors(); + globalErrors.install(); + }; + + if (!options.suppressLoadErrors) { + installGlobalErrors(); + globalErrors.pushListener(function(message, filename, lineno) { + topSuite.result.failedExpectations.push({ + passed: false, + globalErrorType: 'load', + message: message, + filename: filename, + lineno: lineno + }); }); - }); + } this.specFilter = function() { return true; @@ -193,13 +205,6 @@ getJasmineRequireObj().Env = function(j$) { return seed; }; - this.suppressLoadErrors = function() { - if (handlingLoadErrors) { - globalErrors.popListener(); - } - handlingLoadErrors = false; - }; - this.deprecated = function(deprecation) { var runnable = currentRunnable() || topSuite; runnable.addDeprecationWarning(deprecation); @@ -309,7 +314,7 @@ getJasmineRequireObj().Env = function(j$) { this.execute = function(runnablesToRun) { var self = this; - this.suppressLoadErrors(); + installGlobalErrors(); if(!runnablesToRun) { if (focusedRunnables.length) {