Skip to content

Commit 799d920

Browse files
author
Giovanni Bucci
authored
assert: show diff when doing partial comparisons
PR-URL: #56211 Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ea493c1 commit 799d920

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

‎lib/assert.js‎

Lines changed: 3 additions & 4 deletions
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(leti=0;i<keysExpected.length;i++){
641641
constkey=keysExpected[i];
642-
assert(
643-
ReflectHas(actual,key),
644-
newAssertionError({message: `Expected key ${String(key)} not found in actual object`}),
645-
);
642+
if(!ReflectHas(actual,key)){
643+
returnfalse;
644+
}
646645
if(!compareBranch(actual[key],expected[key],comparedObjects)){
647646
returnfalse;
648647
}

‎lib/internal/assert/assertion_error.js‎

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

2727
constkReadableOperator={
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
constkMaxShortStringLength=12;
4243
constkMaxLongStringLength=512;
4344

45+
constkMethodsWithCustomMessageDiff=['deepStrictEqual','strictEqual','partialDeepStrictEqual'];
46+
4447
functioncopyError(source){
4548
consttarget=ObjectAssign(
4649
{__proto__: ObjectGetPrototypeOf(source)},
@@ -210,9 +213,13 @@ function createErrDiff(actual, expected, operator, customMessage){
210213
constcheckCommaDisparity=actual!=null&&typeofactual==='object';
211214
constdiff=myersDiff(inspectedSplitActual,inspectedSplitExpected,checkCommaDisparity);
212215

213-
constmyersDiffMessage=printMyersDiff(diff);
216+
constmyersDiffMessage=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
}elseif(operator==='notDeepStrictEqual'||
281288
operator==='notStrictEqual'){

‎lib/internal/assert/myers_diff.js‎

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

125-
functionprintMyersDiff(diff,simple=false){
125+
functionprintMyersDiff(diff,operator){
126126
letmessage='';
127127
letskipped=false;
128128
letnopCount=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
}elseif(type==='delete'){
153157
message+=`${colors.red}-${colors.white}${value}\n`;
154158
}elseif(type==='nop'){

‎test/parallel/test-assert.js‎

Lines changed: 11 additions & 0 deletions
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
letthrew=false;
13561367
try{

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

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

5-
assert.throws(()=>{
6+
functionsetup(){
67
process.env.FORCE_COLOR='1';
78
deleteprocess.env.NODE_DISABLE_COLORS;
89
deleteprocess.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
constexpected='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
returntrue;
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+
constexpected='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+
returntrue;
51+
});
52+
53+
assert.throws(()=>{
54+
setup();
55+
assert.partialDeepStrictEqual([1,2,3,5],[4,5]);
56+
},(err)=>{
57+
constexpected='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+
returntrue;
70+
});
71+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-warnings
12
'use strict';
23
require('../common');
34
constassert=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)