From 763a83c83313a55cb531041c3d184b020fe3e174 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Fri, 23 Feb 2018 14:43:47 +1100 Subject: [PATCH 1/3] Display error properties for failed specs --- spec/core/ExceptionFormatterSpec.js | 22 ++++++++++++++++++++ src/core/ExceptionFormatter.js | 32 ++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/spec/core/ExceptionFormatterSpec.js b/spec/core/ExceptionFormatterSpec.js index a7069d3e..0e4f074c 100644 --- a/spec/core/ExceptionFormatterSpec.js +++ b/spec/core/ExceptionFormatterSpec.js @@ -116,5 +116,27 @@ describe("ExceptionFormatter", function() { it("returns null if no Error provided", function() { expect(new jasmineUnderTest.ExceptionFormatter().stack()).toBeNull(); }); + + it("includes error properties in stack", function() { + var error; + try { throw new Error("an error") } catch(e) { error = e; } + error.someProperty = 'hello there'; + + var result = new jasmineUnderTest.ExceptionFormatter().stack(error); + + expect(result).toMatch(/error properties: {/); + expect(result).toMatch(/"someProperty": "hello there"/); + }); + + it("drops error properties if there is a cycle", function() { + var error; + try { throw new Error("an error") } catch(e) { error = e; } + error.someProperty = error; + + var result = new jasmineUnderTest.ExceptionFormatter().stack(error); + + expect(result).not.toMatch(/error properties/); + }); + }); }); diff --git a/src/core/ExceptionFormatter.js b/src/core/ExceptionFormatter.js index e6808403..4daf2a3c 100644 --- a/src/core/ExceptionFormatter.js +++ b/src/core/ExceptionFormatter.js @@ -29,12 +29,16 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { var stackTrace = new j$.StackTrace(error.stack); var lines = filterJasmine(stackTrace); + var result = ''; if (stackTrace.message) { lines.unshift(stackTrace.message); } - return lines.join('\n'); + result += formatProperties(error); + result += lines.join('\n'); + + return result; }; function filterJasmine(stackTrace) { @@ -51,6 +55,32 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { return result; } + + function formatProperties(error) { + if (!(error instanceof Object)) { + return; + } + + var ignored = ['name', 'message', 'stack', 'fileName', 'sourceURL', 'line', 'lineNumber', 'stack']; + var result = {}; + var empty = true; + + for (var prop in error) { + if (ignored.includes(prop)) { + continue; + } + result[prop] = error[prop]; + empty = false; + } + + if (!empty) { + try { + return 'error properties: ' + JSON.stringify(result, null, 2) + '\n'; + } catch (_) {} + } + + return ''; + } } return ExceptionFormatter; From 9ee85c35d2926c26fff1383914512c9cc71b6404 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Tue, 27 Feb 2018 10:21:50 +1100 Subject: [PATCH 2/3] Remove duplicate ignored property --- src/core/ExceptionFormatter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/ExceptionFormatter.js b/src/core/ExceptionFormatter.js index 4daf2a3c..71961776 100644 --- a/src/core/ExceptionFormatter.js +++ b/src/core/ExceptionFormatter.js @@ -61,7 +61,7 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { return; } - var ignored = ['name', 'message', 'stack', 'fileName', 'sourceURL', 'line', 'lineNumber', 'stack']; + var ignored = ['name', 'message', 'stack', 'fileName', 'sourceURL', 'line', 'lineNumber']; var result = {}; var empty = true; From 1149d4edde73abd7aaf396c2bccd037ea9dea160 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Tue, 27 Feb 2018 10:22:06 +1100 Subject: [PATCH 3/3] Use j$.pp instead of JSON.stringify() for pretty printing --- spec/core/ExceptionFormatterSpec.js | 13 +------------ src/core/ExceptionFormatter.js | 4 +--- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/spec/core/ExceptionFormatterSpec.js b/spec/core/ExceptionFormatterSpec.js index 0e4f074c..e896a5fe 100644 --- a/spec/core/ExceptionFormatterSpec.js +++ b/spec/core/ExceptionFormatterSpec.js @@ -124,18 +124,7 @@ describe("ExceptionFormatter", function() { var result = new jasmineUnderTest.ExceptionFormatter().stack(error); - expect(result).toMatch(/error properties: {/); - expect(result).toMatch(/"someProperty": "hello there"/); - }); - - it("drops error properties if there is a cycle", function() { - var error; - try { throw new Error("an error") } catch(e) { error = e; } - error.someProperty = error; - - var result = new jasmineUnderTest.ExceptionFormatter().stack(error); - - expect(result).not.toMatch(/error properties/); + expect(result).toMatch(/error properties:.*someProperty.*hello there/); }); }); diff --git a/src/core/ExceptionFormatter.js b/src/core/ExceptionFormatter.js index 71961776..aa3a62ae 100644 --- a/src/core/ExceptionFormatter.js +++ b/src/core/ExceptionFormatter.js @@ -74,9 +74,7 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { } if (!empty) { - try { - return 'error properties: ' + JSON.stringify(result, null, 2) + '\n'; - } catch (_) {} + return 'error properties: ' + j$.pp(result) + '\n'; } return '';