Skip to content

Commit 28902f3

Browse files
committed
assert: improve diff output
The output is now a tiny bit improved by sorting object properties when inspecting the values that are compared with each other. That reduces the overall diff for identical objects with a different property insertion order. PR-URL: nodejs#22788 Refs: nodejs#22763 Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent b95b0d8 commit 28902f3

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

lib/internal/assert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function inspectValue(val) {
5252
// comparison.
5353
breakLength: Infinity,
5454
// Assert does not detect proxies currently.
55-
showProxy: false
55+
showProxy: false,
56+
sorted: true
5657
}
5758
);
5859
}

test/parallel/test-assert-deep.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function re(literals, ...values) {
2323
depth: 1000,
2424
customInspect: false,
2525
maxArrayLength: Infinity,
26-
breakLength: Infinity
26+
breakLength: Infinity,
27+
sorted: true
2728
});
2829
// Need to escape special characters.
2930
result += str;

test/parallel/test-assert.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ testAssertionMessage(-Infinity, '-Infinity');
293293
testAssertionMessage([1, 2, 3], '[\n+ 1,\n+ 2,\n+ 3\n+ ]');
294294
testAssertionMessage(function f() {}, '[Function: f]');
295295
testAssertionMessage(function() {}, '[Function]');
296-
testAssertionMessage(circular, '{\n+ y: 1,\n+ x: [Circular]\n+ }');
296+
testAssertionMessage(circular, '{\n+ x: [Circular],\n+ y: 1\n+ }');
297297
testAssertionMessage({ a: undefined, b: null },
298298
'{\n+ a: undefined,\n+ b: null\n+ }');
299299
testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
@@ -602,8 +602,8 @@ assert.throws(
602602
'\n' +
603603
'+ {}\n' +
604604
'- {\n' +
605-
"- loop: 'forever',\n" +
606-
'- [Symbol(nodejs.util.inspect.custom)]: [Function]\n' +
605+
'- [Symbol(nodejs.util.inspect.custom)]: [Function],\n' +
606+
"- loop: 'forever'\n" +
607607
'- }'
608608
});
609609

@@ -885,9 +885,12 @@ common.expectsError(
885885
code: 'ERR_ASSERTION',
886886
name: 'AssertionError [ERR_ASSERTION]',
887887
message: `${start}\n${actExp}\n\n` +
888-
" Comparison {\n name: 'TypeError',\n" +
889-
" message: 'Wrong value',\n+ code: 404\n" +
890-
'- code: 404,\n- foo: undefined\n }'
888+
' Comparison {\n' +
889+
' code: 404,\n' +
890+
'- foo: undefined,\n' +
891+
" message: 'Wrong value',\n" +
892+
" name: 'TypeError'\n" +
893+
' }'
891894
}
892895
);
893896

@@ -899,9 +902,13 @@ common.expectsError(
899902
code: 'ERR_ASSERTION',
900903
name: 'AssertionError [ERR_ASSERTION]',
901904
message: `${start}\n${actExp}\n\n` +
902-
" Comparison {\n name: 'TypeError',\n" +
903-
" message: 'Wrong value',\n+ code: 404\n" +
904-
"- code: '404',\n- foo: undefined\n }"
905+
' Comparison {\n' +
906+
'+ code: 404,\n' +
907+
"- code: '404',\n" +
908+
'- foo: undefined,\n' +
909+
" message: 'Wrong value',\n" +
910+
" name: 'TypeError'\n" +
911+
' }'
905912
}
906913
);
907914

@@ -931,8 +938,11 @@ common.expectsError(
931938
name: 'AssertionError [ERR_ASSERTION]',
932939
code: 'ERR_ASSERTION',
933940
message: `${start}\n${actExp}\n\n` +
934-
" Comparison {\n+ name: 'TypeError',\n- name: 'Error'," +
935-
"\n message: 'e'\n }"
941+
' Comparison {\n' +
942+
" message: 'e',\n" +
943+
"+ name: 'TypeError'\n" +
944+
"- name: 'Error'\n" +
945+
' }'
936946
}
937947
);
938948
assert.throws(
@@ -942,8 +952,11 @@ common.expectsError(
942952
code: 'ERR_ASSERTION',
943953
generatedMessage: true,
944954
message: `${start}\n${actExp}\n\n` +
945-
" Comparison {\n name: 'Error',\n+ message: 'foo'" +
946-
"\n- message: ''\n }"
955+
' Comparison {\n' +
956+
"+ message: 'foo',\n" +
957+
"- message: '',\n" +
958+
" name: 'Error'\n" +
959+
' }'
947960
}
948961
);
949962

test/parallel/test-internal-errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ common.expectsError(() => {
9292
}, {
9393
code: 'ERR_ASSERTION',
9494
type: assert.AssertionError,
95-
message: /\+ message: 'Error for testing purposes: a'\n- message: \/\^Error/
95+
message: /\+ message: 'Error for testing purposes: a',\n- message: \/\^Error/
9696
});
9797

9898
// Test ERR_INVALID_FD_TYPE

0 commit comments

Comments
 (0)