diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index cfd1d36a..023efe24 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -709,6 +709,7 @@ getJasmineRequireObj().Env = function(j$) { var throwOnExpectationFailure = false; var random = true; var seed = null; + var suppressLoadErrors = false; var currentSuite = function() { return currentlyExecutingSuites[currentlyExecutingSuites.length - 1]; @@ -772,6 +773,15 @@ getJasmineRequireObj().Env = function(j$) { ]); var globalErrors = new j$.GlobalErrors(); + globalErrors.install(); + globalErrors.pushListener(function(message) { + if (!suppressLoadErrors) { + topSuite.result.failedExpectations.push({ + passed: false, + message: message + }); + } + }); this.specFilter = function() { return true; @@ -915,6 +925,10 @@ getJasmineRequireObj().Env = function(j$) { return seed; }; + this.suppressLoadErrors = function() { + suppressLoadErrors = true; + }; + var queueRunnerFactory = function(options) { options.catchException = catchException; options.clearStack = options.clearStack || clearStack; @@ -941,6 +955,8 @@ getJasmineRequireObj().Env = function(j$) { }; this.execute = function(runnablesToRun) { + globalErrors.popListener(); + if(!runnablesToRun) { if (focusedRunnables.length) { runnablesToRun = focusedRunnables; @@ -996,11 +1012,9 @@ getJasmineRequireObj().Env = function(j$) { currentlyExecutingSuites.push(topSuite); - globalErrors.install(); processor.execute(function() { clearResourcesForRunnable(topSuite.id); currentlyExecutingSuites.pop(); - globalErrors.uninstall(); /** * Information passed to the {@link Reporter#jasmineDone} event. @@ -2398,8 +2412,6 @@ getJasmineRequireObj().GlobalErrors = function(j$) { } }; - this.uninstall = function noop() {}; - this.install = function install() { if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) { var originalHandlers = global.process.listeners('uncaughtException'); diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index b5bcef8a..df737be8 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -1974,4 +1974,64 @@ describe("Env integration", function() { env.execute(); }); + + describe("In a browser", function() { + if (typeof document !== 'undefined') { + it('reports errors that occur during loading', function(done) { + var global = { + setTimeout: function(fn, delay) { setTimeout(fn, delay) }, + clearTimeout: function(fn, delay) { clearTimeout(fn, delay) }, + }; + spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global); + + var env = new jasmineUnderTest.Env(), + reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']); + + reporter.jasmineDone.and.callFake(function(e) { + expect(e.failedExpectations).toEqual([ + { + passed: false, + message: 'Uncaught SyntaxError: Unexpected end of input' + }, + { + passed: false, + message: 'Uncaught Error: ENOCHEESE' + } + ]); + + done(); + }); + + env.addReporter(reporter); + global.onerror('Uncaught SyntaxError: Unexpected end of input'); + global.onerror('Uncaught Error: ENOCHEESE'); + + env.execute(); + }); + } + + describe('If suppressLoadErrors was called', function() { + it('does not report errors that occur during loading', function(done) { + var global = { + setTimeout: function(fn, delay) { setTimeout(fn, delay) }, + clearTimeout: function(fn, delay) { clearTimeout(fn, delay) }, + }; + spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global); + + var env = new jasmineUnderTest.Env(), + reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']); + + reporter.jasmineDone.and.callFake(function(e) { + expect(e.failedExpectations).toEqual([]); + 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 4cbed3d8..d13fdbb6 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -28,6 +28,7 @@ getJasmineRequireObj().Env = function(j$) { var throwOnExpectationFailure = false; var random = true; var seed = null; + var suppressLoadErrors = false; var currentSuite = function() { return currentlyExecutingSuites[currentlyExecutingSuites.length - 1]; @@ -91,6 +92,15 @@ getJasmineRequireObj().Env = function(j$) { ]); var globalErrors = new j$.GlobalErrors(); + globalErrors.install(); + globalErrors.pushListener(function(message) { + if (!suppressLoadErrors) { + topSuite.result.failedExpectations.push({ + passed: false, + message: message + }); + } + }); this.specFilter = function() { return true; @@ -234,6 +244,10 @@ getJasmineRequireObj().Env = function(j$) { return seed; }; + this.suppressLoadErrors = function() { + suppressLoadErrors = true; + }; + var queueRunnerFactory = function(options) { options.catchException = catchException; options.clearStack = options.clearStack || clearStack; @@ -260,6 +274,8 @@ getJasmineRequireObj().Env = function(j$) { }; this.execute = function(runnablesToRun) { + globalErrors.popListener(); + if(!runnablesToRun) { if (focusedRunnables.length) { runnablesToRun = focusedRunnables; @@ -315,11 +331,9 @@ getJasmineRequireObj().Env = function(j$) { currentlyExecutingSuites.push(topSuite); - globalErrors.install(); processor.execute(function() { clearResourcesForRunnable(topSuite.id); currentlyExecutingSuites.pop(); - globalErrors.uninstall(); /** * Information passed to the {@link Reporter#jasmineDone} event. diff --git a/src/core/GlobalErrors.js b/src/core/GlobalErrors.js index ced4a76a..3eaec399 100644 --- a/src/core/GlobalErrors.js +++ b/src/core/GlobalErrors.js @@ -13,8 +13,6 @@ getJasmineRequireObj().GlobalErrors = function(j$) { } }; - this.uninstall = function noop() {}; - this.install = function install() { if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) { var originalHandlers = global.process.listeners('uncaughtException');