diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index dd1b911a4ba3e5..82dae694368c45 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -313,6 +313,16 @@ function createErrDiff(actual, expected, operator) { return `${msg}${skipped ? skippedMsg : ''}\n${res}${other}${end}${indicator}`; } +function addEllipsis(string) { + const lines = StringPrototypeSplit(string, '\n'); + if (lines.length > 10) { + lines.length = 10; + return `${ArrayPrototypeJoin(lines, '\n')}\n...`; + } else if (string.length > 512) { + return `${StringPrototypeSlice(string, 512)}...`; + } +} + class AssertionError extends Error { constructor(options) { validateObject(options, 'options'); @@ -467,16 +477,11 @@ class AssertionError extends Error { const tmpActual = this.actual; const tmpExpected = this.expected; - for (const name of ['actual', 'expected']) { - if (typeof this[name] === 'string') { - const lines = StringPrototypeSplit(this[name], '\n'); - if (lines.length > 10) { - lines.length = 10; - this[name] = `${ArrayPrototypeJoin(lines, '\n')}\n...`; - } else if (this[name].length > 512) { - this[name] = `${StringPrototypeSlice(this[name], 512)}...`; - } - } + if (typeof this.actual === 'string') { + this.actual = addEllipsis(this.actual); + } + if (typeof this.expected === 'string') { + this.expected = addEllipsis(this.expected); } // This limits the `actual` and `expected` property default inspection to