From e848898404f15c8805a2781a59967abc3e40960d Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Tue, 14 Mar 2017 10:51:44 +0000 Subject: [PATCH] Allow error source to be fixed when assertion errors are created If the source is known through other means it doesn't have to be derived from the assertion error's stack trace. --- lib/assert.js | 1 + lib/serialize-error.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 17291f4..12dc9cc 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -16,6 +16,7 @@ class AssertionError extends Error { this.name = 'AssertionError'; this.assertion = opts.assertion; + this.fixedSource = opts.fixedSource; this.operator = opts.operator; this.values = opts.values || []; diff --git a/lib/serialize-error.js b/lib/serialize-error.js index a0053ac..2d2fe00 100644 --- a/lib/serialize-error.js +++ b/lib/serialize-error.js @@ -15,13 +15,15 @@ function filter(propertyName, isRoot) { } const stackUtils = new StackUtils(); -function buildSource(stack) { +function extractSource(stack) { if (!stack) { return null; } const firstStackLine = extractStack(stack).split('\n')[0]; - const source = stackUtils.parseLine(firstStackLine); + return stackUtils.parseLine(firstStackLine); +} +function buildSource(source) { if (!source) { return null; } @@ -49,10 +51,10 @@ module.exports = error => { const stack = typeof error.stack === 'string' ? beautifyStack(error.stack) : null; - const source = buildSource(stack); + const retval = { avaAssertionError: isAvaAssertionError(error), - source + source: buildSource(extractSource(stack)) }; if (stack) { retval.stack = stack; @@ -64,6 +66,13 @@ module.exports = error => { retval.statements = error.statements; retval.values = error.values; + if (error.fixedSource) { + const source = buildSource(error.fixedSource); + if (source) { + retval.source = source; + } + } + if (error.assertion) { retval.assertion = error.assertion; }