Skip to content

Commit 085c344

Browse files
puskinmarco-ippolito
authored andcommitted
assert: show the diff when deep comparing data with a custom message
Fixes: #48465 PR-URL: #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>
1 parent 81ad69d commit 085c344

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ function inspectValue(val) {
7272
);
7373
}
7474

75-
function createErrDiff(actual, expected, operator) {
75+
function getErrorMessage(operator, message) {
76+
return message || kReadableOperator[operator];
77+
}
78+
79+
function createErrDiff(actual, expected, operator, message = '') {
7680
let other = '';
7781
let res = '';
7882
let end = '';
@@ -110,7 +114,7 @@ function createErrDiff(actual, expected, operator) {
110114
if ((typeof actual !== 'object' || actual === null) &&
111115
(typeof expected !== 'object' || expected === null) &&
112116
(actual !== 0 || expected !== 0)) { // -0 === +0
113-
return `${kReadableOperator[operator]}\n\n` +
117+
return `${getErrorMessage(operator, message)}\n\n` +
114118
`${actualLines[0]} !== ${expectedLines[0]}\n`;
115119
}
116120
} else if (operator !== 'strictEqualObject') {
@@ -184,8 +188,7 @@ function createErrDiff(actual, expected, operator) {
184188

185189
let printedLines = 0;
186190
let identical = 0;
187-
const msg = kReadableOperator[operator] +
188-
`\n${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;
191+
const msg = `${getErrorMessage(operator, message)}\n${colors.green}+ actual${colors.white} ${colors.red}- expected${colors.white}`;
189192
const skippedMsg = ` ${colors.blue}...${colors.white} Lines skipped`;
190193

191194
let lines = actualLines;
@@ -337,7 +340,11 @@ class AssertionError extends Error {
337340
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
338341

339342
if (message != null) {
340-
super(String(message));
343+
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
344+
super(createErrDiff(actual, expected, operator, message));
345+
} else {
346+
super(String(message));
347+
}
341348
} else {
342349
// Reset colors on each call to make sure we handle dynamically set environment
343350
// variables correct.

test/parallel/test-assert.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ test('Test assertion messages', () => {
391391
assert.throws(
392392
() => assert.strictEqual(1, 2, 'oh no'),
393393
{
394-
message: 'oh no',
394+
message: 'oh no\n\n1 !== 2\n',
395395
generatedMessage: false
396396
}
397397
);
@@ -1203,7 +1203,7 @@ test('Additional assert', () => {
12031203
),
12041204
{
12051205
actual,
1206-
message,
1206+
message: "message\n+ actual - expected\n\n+ 'foobar'\n- {\n- message: 'foobar'\n- }",
12071207
operator: 'throws',
12081208
generatedMessage: false
12091209
}
@@ -1250,6 +1250,17 @@ test('Additional assert', () => {
12501250
}
12511251
);
12521252

1253+
assert.throws(
1254+
() => {
1255+
assert.deepStrictEqual({ a: true }, { a: false }, 'custom message');
1256+
},
1257+
{
1258+
code: 'ERR_ASSERTION',
1259+
name: 'AssertionError',
1260+
message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }'
1261+
}
1262+
);
1263+
12531264
{
12541265
let threw = false;
12551266
try {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy