Skip to content

Commit b0791bc

Browse files
puskin94RafaelGSS
authored andcommitted
assert: show diff when doing partial comparisons
PR-URL: nodejs#56211 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent d300027 commit b0791bc

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

lib/assert.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,9 @@ function compareBranch(
639639
// Check if all expected keys and values match
640640
for (let i = 0; i < keysExpected.length; i++) {
641641
const key = keysExpected[i];
642-
assert(
643-
ReflectHas(actual, key),
644-
new AssertionError({ message: `Expected key ${String(key)} not found in actual object` }),
645-
);
642+
if (!ReflectHas(actual, key)) {
643+
return false;
644+
}
646645
if (!compareBranch(actual[key], expected[key], comparedObjects)) {
647646
return false;
648647
}

lib/internal/assert/assertion_error.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const { myersDiff, printMyersDiff, printSimpleMyersDiff } = require('internal/as
2626

2727
const kReadableOperator = {
2828
deepStrictEqual: 'Expected values to be strictly deep-equal:',
29+
partialDeepStrictEqual: 'Expected values to be partially and strictly deep-equal:',
2930
strictEqual: 'Expected values to be strictly equal:',
3031
strictEqualObject: 'Expected "actual" to be reference-equal to "expected":',
3132
deepEqual: 'Expected values to be loosely deep-equal:',
@@ -41,6 +42,8 @@ const kReadableOperator = {
4142
const kMaxShortStringLength = 12;
4243
const kMaxLongStringLength = 512;
4344

45+
const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual'];
46+
4447
function copyError(source) {
4548
const target = ObjectAssign(
4649
{ __proto__: ObjectGetPrototypeOf(source) },
@@ -210,9 +213,13 @@ function createErrDiff(actual, expected, operator, customMessage) {
210213
const checkCommaDisparity = actual != null && typeof actual === 'object';
211214
const diff = myersDiff(inspectedSplitActual, inspectedSplitExpected, checkCommaDisparity);
212215

213-
const myersDiffMessage = printMyersDiff(diff);
216+
const myersDiffMessage = printMyersDiff(diff, operator);
214217
message = myersDiffMessage.message;
215218

219+
if (operator === 'partialDeepStrictEqual') {
220+
header = `${colors.gray}${colors.hasColors ? '' : '+ '}actual${colors.white} ${colors.red}- expected${colors.white}`;
221+
}
222+
216223
if (myersDiffMessage.skipped) {
217224
skipped = true;
218225
}
@@ -255,7 +262,7 @@ class AssertionError extends Error {
255262
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
256263

257264
if (message != null) {
258-
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
265+
if (kMethodsWithCustomMessageDiff.includes(operator)) {
259266
super(createErrDiff(actual, expected, operator, message));
260267
} else {
261268
super(String(message));
@@ -275,7 +282,7 @@ class AssertionError extends Error {
275282
expected = copyError(expected);
276283
}
277284

278-
if (operator === 'deepStrictEqual' || operator === 'strictEqual') {
285+
if (kMethodsWithCustomMessageDiff.includes(operator)) {
279286
super(createErrDiff(actual, expected, operator, message));
280287
} else if (operator === 'notDeepStrictEqual' ||
281288
operator === 'notStrictEqual') {

lib/internal/assert/myers_diff.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function printSimpleMyersDiff(diff) {
122122
return `\n${message}`;
123123
}
124124

125-
function printMyersDiff(diff, simple = false) {
125+
function printMyersDiff(diff, operator) {
126126
let message = '';
127127
let skipped = false;
128128
let nopCount = 0;
@@ -148,7 +148,11 @@ function printMyersDiff(diff, simple = false) {
148148
}
149149

150150
if (type === 'insert') {
151-
message += `${colors.green}+${colors.white} ${value}\n`;
151+
if (operator === 'partialDeepStrictEqual') {
152+
message += `${colors.gray}${colors.hasColors ? ' ' : '+'} ${value}${colors.white}\n`;
153+
} else {
154+
message += `${colors.green}+${colors.white} ${value}\n`;
155+
}
152156
} else if (type === 'delete') {
153157
message += `${colors.red}-${colors.white} ${value}\n`;
154158
} else if (type === 'nop') {

test/parallel/test-assert.js

+11
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,17 @@ test('Additional assert', () => {
13511351
}
13521352
);
13531353

1354+
assert.throws(
1355+
() => {
1356+
assert.partialDeepStrictEqual({ a: true }, { a: false }, 'custom message');
1357+
},
1358+
{
1359+
code: 'ERR_ASSERTION',
1360+
name: 'AssertionError',
1361+
message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }\n'
1362+
}
1363+
);
1364+
13541365
{
13551366
let threw = false;
13561367
try {

test/pseudo-tty/test-assert-colors.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
// Flags: --no-warnings
12
'use strict';
23
require('../common');
34
const assert = require('assert').strict;
45

5-
assert.throws(() => {
6+
function setup() {
67
process.env.FORCE_COLOR = '1';
78
delete process.env.NODE_DISABLE_COLORS;
89
delete process.env.NO_COLOR;
10+
}
11+
12+
assert.throws(() => {
13+
setup();
914
assert.deepStrictEqual([1, 2, 2, 2, 2], [2, 2, 2, 2, 2]);
1015
}, (err) => {
1116
const expected = 'Expected values to be strictly deep-equal:\n' +
@@ -19,6 +24,48 @@ assert.throws(() => {
1924
'\x1B[39m 2,\n' +
2025
'\x1B[31m-\x1B[39m 2\n' +
2126
'\x1B[39m ]\n';
27+
2228
assert.strictEqual(err.message, expected);
2329
return true;
2430
});
31+
32+
{
33+
assert.throws(() => {
34+
setup();
35+
assert.partialDeepStrictEqual({ a: 1, b: 2, c: 3, d: 5 }, { z: 4, b: 5 });
36+
}, (err) => {
37+
const expected = 'Expected values to be partially and strictly deep-equal:\n' +
38+
'\x1B[90mactual\x1B[39m \x1B[31m- expected\x1B[39m\n' +
39+
'\n' +
40+
'\x1B[39m {\n' +
41+
'\x1B[90m a: 1,\x1B[39m\n' +
42+
'\x1B[90m b: 2,\x1B[39m\n' +
43+
'\x1B[90m c: 3,\x1B[39m\n' +
44+
'\x1B[90m d: 5\x1B[39m\n' +
45+
'\x1B[31m-\x1B[39m b: 5,\n' +
46+
'\x1B[31m-\x1B[39m z: 4\n' +
47+
'\x1B[39m }\n';
48+
49+
assert.strictEqual(err.message, expected);
50+
return true;
51+
});
52+
53+
assert.throws(() => {
54+
setup();
55+
assert.partialDeepStrictEqual([1, 2, 3, 5], [4, 5]);
56+
}, (err) => {
57+
const expected = 'Expected values to be partially and strictly deep-equal:\n' +
58+
'\x1B[90mactual\x1B[39m \x1B[31m- expected\x1B[39m\n' +
59+
'\n' +
60+
'\x1B[39m [\n' +
61+
'\x1B[90m 1,\x1B[39m\n' +
62+
'\x1B[90m 2,\x1B[39m\n' +
63+
'\x1B[90m 3,\x1B[39m\n' +
64+
'\x1B[31m-\x1B[39m 4,\n' +
65+
'\x1B[39m 5\n' +
66+
'\x1B[39m ]\n';
67+
68+
assert.strictEqual(err.message, expected);
69+
return true;
70+
});
71+
}

test/pseudo-tty/test-assert-no-color.js

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-warnings
12
'use strict';
23
require('../common');
34
const assert = require('assert').strict;
@@ -17,3 +18,19 @@ assert.throws(
1718
'- foo: \'bar\'\n' +
1819
'- }\n',
1920
});
21+
22+
{
23+
assert.throws(
24+
() => {
25+
assert.partialDeepStrictEqual({}, { foo: 'bar' });
26+
},
27+
{
28+
message: 'Expected values to be partially and strictly deep-equal:\n' +
29+
'+ actual - expected\n' +
30+
'\n' +
31+
'+ {}\n' +
32+
'- {\n' +
33+
"- foo: 'bar'\n" +
34+
'- }\n',
35+
});
36+
}

0 commit comments

Comments
 (0)