From 6a6fa7b29a189dff14b78ea0b0bd5e1f7040a86a Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Fri, 22 Mar 2024 09:15:22 -0700 Subject: [PATCH] Tests for existing handling of non-Error global errors in Node --- spec/core/GlobalErrorsSpec.js | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/spec/core/GlobalErrorsSpec.js b/spec/core/GlobalErrorsSpec.js index 68189e90..e87376a0 100644 --- a/spec/core/GlobalErrorsSpec.js +++ b/spec/core/GlobalErrorsSpec.js @@ -328,6 +328,62 @@ describe('GlobalErrors', function() { }); }); + describe('Reporting uncaught exceptions in node.js', function() { + it('prepends a descriptive message when the error is not an `Error`', function() { + const fakeGlobal = { + process: { + on: jasmine.createSpy('process.on'), + removeListener: function() {}, + listeners: function() { + return []; + }, + removeAllListeners: function() {} + } + }; + const handler = jasmine.createSpy('errorHandler'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + + errors.install(); + errors.pushListener(handler); + + uncaughtExceptionListener(fakeGlobal)(17); + + expect(handler).toHaveBeenCalledWith(new Error('Uncaught exception: 17')); + }); + + it('substitutes a descriptive message when the error is falsy', function() { + const fakeGlobal = { + process: { + on: jasmine.createSpy('process.on'), + removeListener: function() {}, + listeners: function() { + return []; + }, + removeAllListeners: function() {} + } + }; + const handler = jasmine.createSpy('errorHandler'); + const errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); + + errors.install(); + errors.pushListener(handler); + + uncaughtExceptionListener(fakeGlobal)(); + + expect(handler).toHaveBeenCalledWith( + new Error('Uncaught exception with no error or message') + ); + }); + + function uncaughtExceptionListener(global) { + // Grab the right listener + expect(global.process.on.calls.argsFor(0)[0]).toEqual( + 'uncaughtException' + ); + return global.process.on.calls.argsFor(0)[1]; + } + }); + describe('#setOverrideListener', function() { it('overrides the existing handlers in browsers until removed', function() { const fakeGlobal = browserGlobal();