diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 396c15a7..a2fc9494 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -3580,7 +3580,7 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { lines.unshift(stackTrace.message); } - if (error.cause) { + if (error.cause && error.cause instanceof Error) { const substack = this.stack_(error.cause, { messageHandling: 'require' }); diff --git a/spec/core/ExceptionFormatterSpec.js b/spec/core/ExceptionFormatterSpec.js index 328945b5..97f2c254 100644 --- a/spec/core/ExceptionFormatterSpec.js +++ b/spec/core/ExceptionFormatterSpec.js @@ -291,6 +291,26 @@ describe('ExceptionFormatter', function() { .withContext('first root cause stack frame') .toContain('ExceptionFormatterSpec.js'); }); + + it('does not throw if cause is a non Error', function() { + const formatter = new jasmineUnderTest.ExceptionFormatter(); + + expect(function() { + formatter.stack( + new Error('error', { + cause: function() {} + }) + ); + }).not.toThrowError(); + + expect(function() { + formatter.stack( + new Error('error', { + cause: 'another error' + }) + ); + }).not.toThrowError(); + }); }); }); }); diff --git a/src/core/ExceptionFormatter.js b/src/core/ExceptionFormatter.js index 0d69458b..1b78de0f 100644 --- a/src/core/ExceptionFormatter.js +++ b/src/core/ExceptionFormatter.js @@ -67,7 +67,7 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { lines.unshift(stackTrace.message); } - if (error.cause) { + if (error.cause && error.cause instanceof Error) { const substack = this.stack_(error.cause, { messageHandling: 'require' });