Skip to content

Commit be26c76

Browse files
committed
assert: improve the strict equal messages
In case reference (un)equal objects fail in assertion, it should be clear that it is about the reference equality and not about the object properties. This is fixed by improving the message in such cases. Refs: #22763 PR-URL: #23056 Refs: #22763 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent a58f377 commit be26c76

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

lib/internal/assert.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ let white = '';
1313
const kReadableOperator = {
1414
deepStrictEqual: 'Expected inputs to be strictly deep-equal:',
1515
strictEqual: 'Expected inputs to be strictly equal:',
16+
strictEqualObject: 'Expected "actual" to be reference-equal to "expected":',
1617
deepEqual: 'Expected inputs to be loosely deep-equal:',
1718
equal: 'Expected inputs to be loosely equal:',
1819
notDeepStrictEqual: 'Expected "actual" not to be strictly deep-equal to:',
1920
notStrictEqual: 'Expected "actual" to be strictly unequal to:',
21+
// eslint-disable-next-line max-len
22+
notStrictEqualObject: 'Expected "actual" not to be reference-equal to "expected":',
2023
notDeepEqual: 'Expected "actual" not to be loosely deep-equal to:',
2124
notEqual: 'Expected "actual" to be loosely unequal to:',
2225
notIdentical: 'Inputs identical but not reference equal:',
@@ -67,13 +70,20 @@ function createErrDiff(actual, expected, operator) {
6770
const actualInspected = inspectValue(actual);
6871
const actualLines = actualInspected.split('\n');
6972
const expectedLines = inspectValue(expected).split('\n');
70-
const msg = kReadableOperator[operator] +
71-
`\n${green}+ actual${white} ${red}- expected${white}`;
72-
const skippedMsg = ` ${blue}...${white} Lines skipped`;
7373

7474
let i = 0;
7575
let indicator = '';
7676

77+
// In case both inputs are objects explicitly mark them as not reference equal
78+
// for the `strictEqual` operator.
79+
if (operator === 'strictEqual' &&
80+
typeof actual === 'object' &&
81+
typeof expected === 'object' &&
82+
actual !== null &&
83+
expected !== null) {
84+
operator = 'strictEqualObject';
85+
}
86+
7787
// If "actual" and "expected" fit on a single line and they are not strictly
7888
// equal, check further special handling.
7989
if (actualLines.length === 1 && expectedLines.length === 1 &&
@@ -89,7 +99,7 @@ function createErrDiff(actual, expected, operator) {
8999
return `${kReadableOperator[operator]}\n\n` +
90100
`${actualLines[0]} !== ${expectedLines[0]}\n`;
91101
}
92-
} else {
102+
} else if (operator !== 'strictEqualObject') {
93103
// If the stderr is a tty and the input length is lower than the current
94104
// columns per line, add a mismatch indicator below the output. If it is
95105
// not a tty, use a default value of 80 characters.
@@ -156,6 +166,10 @@ function createErrDiff(actual, expected, operator) {
156166
}
157167

158168
let printedLines = 0;
169+
const msg = kReadableOperator[operator] +
170+
`\n${green}+ actual${white} ${red}- expected${white}`;
171+
const skippedMsg = ` ${blue}...${white} Lines skipped`;
172+
159173
for (i = 0; i < maxLines; i++) {
160174
// Only extra expected lines exist
161175
const cur = i - lastPos;
@@ -274,8 +288,15 @@ class AssertionError extends Error {
274288
operator === 'notStrictEqual') {
275289
// In case the objects are equal but the operator requires unequal, show
276290
// the first object and say A equals B
291+
let base = kReadableOperator[operator];
277292
const res = inspectValue(actual).split('\n');
278-
const base = kReadableOperator[operator];
293+
294+
// In case "actual" is an object, it should not be reference equal.
295+
if (operator === 'notStrictEqual' &&
296+
typeof actual === 'object' &&
297+
actual !== null) {
298+
base = kReadableOperator.notStrictEqualObject;
299+
}
279300

280301
// Only remove lines in case it makes sense to collapse those.
281302
// TODO: Accept env to always show the full error.

test/parallel/test-assert.js

+44-3
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ assert.throws(
415415
{
416416
code: 'ERR_ASSERTION',
417417
name: 'AssertionError [ERR_ASSERTION]',
418-
message: strictEqualMessageStart +
418+
message: 'Expected "actual" to be reference-equal to "expected":\n' +
419419
'+ actual - expected\n\n' +
420-
'+ [Error: foo]\n- [Error: foobar]\n ^'
420+
'+ [Error: foo]\n- [Error: foobar]'
421421
}
422422
);
423423

@@ -1022,7 +1022,8 @@ assert.throws(
10221022
assert.throws(
10231023
() => assert.strictEqual(args, { 0: 'a' }),
10241024
{
1025-
message: `${strictEqualMessageStart}+ actual - expected\n\n` +
1025+
message: 'Expected "actual" to be reference-equal to "expected":\n' +
1026+
'+ actual - expected\n\n' +
10261027
"+ [Arguments] {\n- {\n '0': 'a'\n }"
10271028
}
10281029
);
@@ -1091,3 +1092,43 @@ assert.throws(
10911092
}
10921093
);
10931094
}
1095+
1096+
// Indicate where the strings diverge.
1097+
assert.throws(
1098+
() => assert.strictEqual('test test', 'test foobar'),
1099+
{
1100+
code: 'ERR_ASSERTION',
1101+
name: 'AssertionError [ERR_ASSERTION]',
1102+
message: strictEqualMessageStart +
1103+
'+ actual - expected\n\n' +
1104+
"+ 'test test'\n" +
1105+
"- 'test foobar'\n" +
1106+
' ^'
1107+
}
1108+
);
1109+
1110+
// Check for reference equal objects in `notStrictEqual()`
1111+
assert.throws(
1112+
() => {
1113+
const obj = {};
1114+
assert.notStrictEqual(obj, obj);
1115+
},
1116+
{
1117+
code: 'ERR_ASSERTION',
1118+
name: 'AssertionError [ERR_ASSERTION]',
1119+
message: 'Expected "actual" not to be reference-equal to "expected": {}'
1120+
}
1121+
);
1122+
1123+
assert.throws(
1124+
() => {
1125+
const obj = { a: true };
1126+
assert.notStrictEqual(obj, obj);
1127+
},
1128+
{
1129+
code: 'ERR_ASSERTION',
1130+
name: 'AssertionError [ERR_ASSERTION]',
1131+
message: 'Expected "actual" not to be reference-equal to "expected":\n\n' +
1132+
'{\n a: true\n}\n'
1133+
}
1134+
);

0 commit comments

Comments
 (0)