assert: refine assertion message · nodejs/node@3367bad · GitHub
Skip to content

Commit 3367bad

Browse files
BridgeARtargos
authored andcommitted
assert: refine assertion message
This makes sure that the error message is more appropriate than before by checking closer what operator is used and which is not. It also increases the total number of lines printed to the user. PR-URL: #27525 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent e573c99 commit 3367bad

4 files changed

Lines changed: 74 additions & 45 deletions

File tree

lib/internal/assert/assertion_error.js

Lines changed: 35 additions & 28 deletions

test/parallel/test-assert-deep.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function assertDeepAndStrictEqual(a, b) {
195195
function assertNotDeepOrStrict(a, b, err) {
196196
assert.throws(
197197
() => assert.deepEqual(a, b),
198-
err || re`${a}\n\nshould equal\n\n${b}`
198+
err || re`${a}\n\nshould loosely deep-equal\n\n${b}`
199199
);
200200
assert.throws(
201201
() => assert.deepStrictEqual(a, b),
@@ -204,7 +204,7 @@ function assertNotDeepOrStrict(a, b, err) {
204204

205205
assert.throws(
206206
() => assert.deepEqual(b, a),
207-
err || re`${b}\n\nshould equal\n\n${a}`
207+
err || re`${b}\n\nshould loosely deep-equal\n\n${a}`
208208
);
209209
assert.throws(
210210
() => assert.deepStrictEqual(b, a),
@@ -651,6 +651,20 @@ assertDeepAndStrictEqual(-0, -0);
651651
assertDeepAndStrictEqual(a, b);
652652
}
653653

654+
assert.throws(
655+
() => assert.notDeepEqual(1, true),
656+
{
657+
message: /1\n\nshould not loosely deep-equal\n\ntrue/
658+
}
659+
);
660+
661+
assert.throws(
662+
() => assert.notDeepEqual(1, 1),
663+
{
664+
message: /Expected "actual" not to be loosely deep-equal to:\n\n1/
665+
}
666+
);
667+
654668
assert.deepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14));
655669

656670
assert.throws(() => { assert.deepEqual(new Date(), new Date(2000, 3, 14)); },
@@ -779,7 +793,7 @@ assert.throws(
779793
() => assert.notDeepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
780794
{
781795
name: 'AssertionError',
782-
message: 'Expected "actual" not to be strictly deep-equal to: ' +
796+
message: 'Expected "actual" not to be strictly deep-equal to:\n\n' +
783797
util.inspect(new Date(2000, 3, 14))
784798
}
785799
);
@@ -815,11 +829,11 @@ assert.throws(
815829
message: `${defaultMsgStartFull}\n\n+ /a/m\n- /a/`
816830
});
817831
assert.throws(
818-
() => assert.deepStrictEqual(/a/igm, /a/im),
832+
() => assert.deepStrictEqual(/aa/igm, /aa/im),
819833
{
820834
code: 'ERR_ASSERTION',
821835
name: 'AssertionError',
822-
message: `${defaultMsgStartFull}\n\n+ /a/gim\n- /a/im\n ^`
836+
message: `${defaultMsgStartFull}\n\n+ /aa/gim\n- /aa/im\n ^`
823837
});
824838

825839
{
@@ -939,12 +953,12 @@ assert.deepStrictEqual(obj1, obj2);
939953
}
940954

941955
// Strict equal with identical objects that are not identical
942-
// by reference and longer than 30 elements
956+
// by reference and longer than 50 elements
943957
// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
944958
{
945959
const a = {};
946960
const b = {};
947-
for (let i = 0; i < 35; i++) {
961+
for (let i = 0; i < 55; i++) {
948962
a[`symbol${i}`] = Symbol();
949963
b[`symbol${i}`] = Symbol();
950964
}

test/parallel/test-assert.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,20 @@ assert.throws(
8080
assert.throws(
8181
() => a.notStrictEqual('a '.repeat(30), 'a '.repeat(30)),
8282
{
83-
message: 'Expected "actual" to be strictly unequal to: ' +
83+
message: 'Expected "actual" to be strictly unequal to:\n\n' +
8484
`'${'a '.repeat(30)}'`,
8585
name: 'AssertionError'
8686
}
8787
);
8888

89+
assert.throws(
90+
() => a.notEqual(1, 1),
91+
{
92+
message: '1 != 1',
93+
operator: '!='
94+
}
95+
);
96+
8997
a.notStrictEqual(2, '2');
9098

9199
// Testing the throwing.
@@ -281,15 +289,15 @@ testShortAssertionMessage('a', '"a"');
281289
testShortAssertionMessage('foo', '\'foo\'');
282290
testShortAssertionMessage(0, '0');
283291
testShortAssertionMessage(Symbol(), 'Symbol()');
292+
testShortAssertionMessage(undefined, 'undefined');
293+
testShortAssertionMessage(-Infinity, '-Infinity');
294+
testShortAssertionMessage(function() {}, '[Function]');
284295
testAssertionMessage([], '[]');
285296
testAssertionMessage(/a/, '/a/');
286297
testAssertionMessage(/abc/gim, '/abc/gim');
287298
testAssertionMessage({}, '{}');
288-
testAssertionMessage(undefined, 'undefined');
289-
testAssertionMessage(-Infinity, '-Infinity');
290299
testAssertionMessage([1, 2, 3], '[\n+ 1,\n+ 2,\n+ 3\n+ ]');
291300
testAssertionMessage(function f() {}, '[Function: f]');
292-
testAssertionMessage(function() {}, '[Function]');
293301
testAssertionMessage(circular, '{\n+ x: [Circular],\n+ y: 1\n+ }');
294302
testAssertionMessage({ a: undefined, b: null },
295303
'{\n+ a: undefined,\n+ b: null\n+ }');
@@ -579,12 +587,12 @@ assert.throws(
579587
`${actExp} ... Lines skipped\n` +
580588
'\n' +
581589
' [\n' +
582-
'+ 1,\n'.repeat(10) +
590+
'+ 1,\n'.repeat(25) +
583591
'...\n' +
584-
'- 2,\n'.repeat(10) +
592+
'- 2,\n'.repeat(25) +
585593
'...';
586594
assert.throws(
587-
() => assert.deepEqual(Array(12).fill(1), Array(12).fill(2)),
595+
() => assert.deepEqual(Array(28).fill(1), Array(28).fill(2)),
588596
{ message });
589597

590598
const obj1 = {};
@@ -612,8 +620,8 @@ assert.throws(
612620
);
613621

614622
message = 'Expected "actual" not to be strictly deep-equal to:' +
615-
`\n\n[${'\n 1,'.repeat(25)}\n...\n`;
616-
const data = Array(31).fill(1);
623+
`\n\n[${'\n 1,'.repeat(45)}\n...\n`;
624+
const data = Array(51).fill(1);
617625
assert.throws(
618626
() => assert.notDeepEqual(data, data),
619627
{ message });

test/pseudo-tty/test-assert-position-indicator.js

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)