Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1ee7ce6

Browse filesBrowse files
committed
assert: limit string inspection when logging assertion errors
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>
1 parent ecb963d commit 1ee7ce6
Copy full SHA for 1ee7ce6

File tree

Expand file treeCollapse file tree

1 file changed

+27
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+27
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/assert/assertion_error.js‎

Copy file name to clipboardExpand all lines: lib/internal/assert/assertion_error.js
+27-1Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,37 @@ class AssertionError extends Error {
429429
}
430430

431431
[inspect.custom](recurseTimes, ctx) {
432+
// Long strings should not be fully inspected.
433+
const tmpActual = this.actual;
434+
const tmpExpected = this.expected;
435+
436+
for (const name of ['actual', 'expected']) {
437+
if (typeof this[name] === 'string') {
438+
const lines = this[name].split('\n');
439+
if (lines.length > 10) {
440+
lines.length = 10;
441+
this[name] = `${lines.join('\n')}\n...`;
442+
} else if (this[name].length > 512) {
443+
this[name] = `${this[name].slice(512)}...`;
444+
}
445+
}
446+
}
447+
432448
// This limits the `actual` and `expected` property default inspection to
433449
// the minimum depth. Otherwise those values would be too verbose compared
434450
// to the actual error message which contains a combined view of these two
435451
// input values.
436-
return inspect(this, { ...ctx, customInspect: false, depth: 0 });
452+
const result = inspect(this, {
453+
...ctx,
454+
customInspect: false,
455+
depth: 0
456+
});
457+
458+
// Reset the properties after inspection.
459+
this.actual = tmpActual;
460+
this.expected = tmpExpected;
461+
462+
return result;
437463
}
438464
}
439465

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.