mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
assert: show the diff when deep comparing data with a custom message
Fixes: https://github.com/nodejs/node/issues/48465 PR-URL: https://github.com/nodejs/node/pull/54759 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
6d1cd506b5
commit
e1d8b4f038
2 files changed
+25
-7
No files matched your search
@@ -72,7 +72,11 @@ function inspectValue(val) {
|
||||
);
|
||||
}
|
||||
|
||||
function createErrDiff(actual, expected, operator) {
|
||||
function getErrorMessage(operator, message) {
|
||||
return message || kReadableOperator[operator];
|
||||
}
|
||||
|
||||
function createErrDiff(actual, expected, operator, message = '') {
|
||||
let other = '';
|
||||
let res = '';
|
||||
let end = '';
|
||||
@@ -110,7 +114,7 @@ function createErrDiff(actual, expected, operator) {
|
||||
if ((typeof actual !== 'object' || actual === null) &&
|
||||
(typeof expected !== 'object' || expected === null) &&
|
||||
(actual !== 0 || expected !== 0)) { // -0 === +0
|
||||
return `${kReadableOperator[operator]}\n\n` +
|
||||
return `${getErrorMessage(operator, message)}\n\n` +
|
||||
`${actualLines[0]} !== ${expectedLines[0]}\n`;
|
||||
}
|
||||
} else if (operator !== 'strictEqualObject') {
|
||||
@@ -184,8 +188,7 @@ function createErrDiff(actual, expected, operator) {
|
||||
|
||||
let printedLines = 0;
|
||||
let identical = 0;
|
||||
const msg = kReadableOperator[operator] +
|
||||
`\n${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;
|
||||
const msg = `${getErrorMessage(operator, message)}\n${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;
|
||||
const skippedMsg = ` ${colors.blue}...${colors.white} Lines skipped`;
|
||||
|
||||
let lines = actualLines;
|
||||
@@ -337,7 +340,11 @@ class AssertionError extends Error {
|
||||
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
|
||||
|
||||
if (message != null) {
|
||||
super(String(message));
|
||||
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
|
||||
super(createErrDiff(actual, expected, operator, message));
|
||||
} else {
|
||||
super(String(message));
|
||||
}
|
||||
} else {
|
||||
// Reset colors on each call to make sure we handle dynamically set environment
|
||||
// variables correct.
|
||||
|
||||
Reference in new issue
Block a user