Skip to content

Commit

Permalink
assert: limit string inspection when logging assertion errors
Browse files Browse the repository at this point in the history
This makes sure long strings as `actual` or `expected` values on an
`AssertionError` won't be logged completely. This is important as
the actual value is somewhat redundant in combination with the error
message which already logs the difference between the input values.

PR-URL: #28058
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR committed Jun 17, 2019
1 parent ecb963d commit 1ee7ce6
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,37 @@ class AssertionError extends Error {
}

[inspect.custom](recurseTimes, ctx) {
// Long strings should not be fully inspected.
const tmpActual = this.actual;
const tmpExpected = this.expected;

for (const name of ['actual', 'expected']) {
if (typeof this[name] === 'string') {
const lines = this[name].split('\n');
if (lines.length > 10) {
lines.length = 10;
this[name] = `${lines.join('\n')}\n...`;
} else if (this[name].length > 512) {
this[name] = `${this[name].slice(512)}...`;
}
}
}

// This limits the `actual` and `expected` property default inspection to
// the minimum depth. Otherwise those values would be too verbose compared
// to the actual error message which contains a combined view of these two
// input values.
return inspect(this, { ...ctx, customInspect: false, depth: 0 });
const result = inspect(this, {
...ctx,
customInspect: false,
depth: 0
});

// Reset the properties after inspection.
this.actual = tmpActual;
this.expected = tmpExpected;

return result;
}
}

Expand Down

0 comments on commit 1ee7ce6

Please sign in to comment.