From 10f1220e55c84c5cbcdac9e152b5d11c9e4a71c2 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Mon, 8 May 2017 11:09:32 -0700 Subject: [PATCH] Don't mask errors that occur when no handlers are installed It's possible for async code to cause an error when Jasmine doesn't have any listeners registered internally. This causes Jasmine to crash (Node) or log to the console (browser) because of trying to call the nonexistent handler. This change doesn't fix the overall problem but it does ensure that the original error is logged rather than Jasmine's internal error. --- spec/core/GlobalErrorsSpec.js | 16 ++++++++++++++++ src/core/GlobalErrors.js | 7 ++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spec/core/GlobalErrorsSpec.js b/spec/core/GlobalErrorsSpec.js index a16cc005..56ea1a20 100644 --- a/spec/core/GlobalErrorsSpec.js +++ b/spec/core/GlobalErrorsSpec.js @@ -62,6 +62,22 @@ describe("GlobalErrors", function() { expect(fakeGlobal.onerror).toBe(originalCallback); }); + it("rethrows the original error when there is no handler", function() { + var fakeGlobal = { }, + errors = new jasmineUnderTest.GlobalErrors(fakeGlobal), + originalError = new Error('nope'); + + errors.install(); + + try { + fakeGlobal.onerror(originalError); + } catch (e) { + expect(e).toBe(originalError); + } + + errors.uninstall(); + }); + it("works in node.js", function() { var fakeGlobal = { process: { diff --git a/src/core/GlobalErrors.js b/src/core/GlobalErrors.js index 4f253329..ced4a76a 100644 --- a/src/core/GlobalErrors.js +++ b/src/core/GlobalErrors.js @@ -5,7 +5,12 @@ getJasmineRequireObj().GlobalErrors = function(j$) { var onerror = function onerror() { var handler = handlers[handlers.length - 1]; - handler.apply(null, Array.prototype.slice.call(arguments, 0)); + + if (handler) { + handler.apply(null, Array.prototype.slice.call(arguments, 0)); + } else { + throw arguments[0]; + } }; this.uninstall = function noop() {};