Skip to content

Commit e361951

Browse files
puskintargos
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 bb34990 commit e361951

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
);
@@ -1204,7 +1204,7 @@ test('Additional assert', () => {
12041204
),
12051205
{
12061206
actual,
1207-
message,
1207+
message: "message\n+ actual - expected\n\n+ 'foobar'\n- {\n- message: 'foobar'\n- }",
12081208
operator: 'throws',
12091209
generatedMessage: false
12101210
}
@@ -1251,6 +1251,17 @@ test('Additional assert', () => {
12511251
}
12521252
);
12531253

1254+
assert.throws(
1255+
() => {
1256+
assert.deepStrictEqual({ a: true }, { a: false }, 'custom message');
1257+
},
1258+
{
1259+
code: 'ERR_ASSERTION',
1260+
name: 'AssertionError',
1261+
message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }'
1262+
}
1263+
);
1264+
12541265
{
12551266
let threw = false;
12561267
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