Display error properties for failed specs

This commit is contained in:
James Bunton
2018-02-23 14:43:47 +11:00
parent a9a112e88f
commit 763a83c833
2 changed files with 53 additions and 1 deletions

View File

@@ -116,5 +116,27 @@ describe("ExceptionFormatter", function() {
it("returns null if no Error provided", function() { it("returns null if no Error provided", function() {
expect(new jasmineUnderTest.ExceptionFormatter().stack()).toBeNull(); 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/);
});
}); });
}); });

View File

@@ -29,12 +29,16 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) {
var stackTrace = new j$.StackTrace(error.stack); var stackTrace = new j$.StackTrace(error.stack);
var lines = filterJasmine(stackTrace); var lines = filterJasmine(stackTrace);
var result = '';
if (stackTrace.message) { if (stackTrace.message) {
lines.unshift(stackTrace.message); lines.unshift(stackTrace.message);
} }
return lines.join('\n'); result += formatProperties(error);
result += lines.join('\n');
return result;
}; };
function filterJasmine(stackTrace) { function filterJasmine(stackTrace) {
@@ -51,6 +55,32 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) {
return result; 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; return ExceptionFormatter;