Skip to content

Commit 0518b9e

Browse files
committed
assert: multiple improvements
1) Switched + / - and red / green in diffs. It seems like that style is more natural to most people. 2) Short primitives do not use the diff anymore. Especially short numbers can be read well like 1 !== 2. Cases that can not be displayed like that (e.g., -0 and +0) use the regular diff output. 3) Improved error descriptions. It was not always clear what the messages stood for. That should now be resolved. 4) Added a position indicator for single lines in case a tty is used and the line is shorter than the visual columns. 5) Color detection is now done by checking stderr instead of stdout. PR-URL: #21628 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
1 parent a2ec808 commit 0518b9e

10 files changed

+281
-218
lines changed

doc/api/assert.md

+56-44
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ Example error diff:
108108
const assert = require('assert').strict;
109109

110110
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
111-
// AssertionError: Input A expected to strictly deep-equal input B:
112-
// + expected - actual ... Lines skipped
111+
// AssertionError: Expected inputs to be strictly deep-equal:
112+
// + actual - expected ... Lines skipped
113113
//
114114
// [
115115
// [
116116
// ...
117117
// 2,
118-
// - 3
119-
// + '3'
118+
// + 3
119+
// - '3'
120120
// ],
121121
// ...
122122
// 5
@@ -315,11 +315,12 @@ const assert = require('assert').strict;
315315

316316
// This fails because 1 !== '1'.
317317
assert.deepStrictEqual({ a: 1 }, { a: '1' });
318-
// AssertionError: Input A expected to strictly deep-equal input B:
319-
// + expected - actual
318+
// AssertionError: Expected inputs to be strictly deep-equal:
319+
// + actual - expected
320+
//
320321
// {
321-
// - a: 1
322-
// + a: '1'
322+
// + a: 1
323+
// - a: '1'
323324
// }
324325

325326
// The following objects don't have own properties
@@ -330,27 +331,30 @@ Object.setPrototypeOf(fakeDate, Date.prototype);
330331

331332
// Different [[Prototype]]:
332333
assert.deepStrictEqual(object, fakeDate);
333-
// AssertionError: Input A expected to strictly deep-equal input B:
334-
// + expected - actual
335-
// - {}
336-
// + Date {}
334+
// AssertionError: Expected inputs to be strictly deep-equal:
335+
// + actual - expected
336+
//
337+
// + {}
338+
// - Date {}
337339

338340
// Different type tags:
339341
assert.deepStrictEqual(date, fakeDate);
340-
// AssertionError: Input A expected to strictly deep-equal input B:
341-
// + expected - actual
342-
// - 2018-04-26T00:49:08.604Z
343-
// + Date {}
342+
// AssertionError: Expected inputs to be strictly deep-equal:
343+
// + actual - expected
344+
//
345+
// + 2018-04-26T00:49:08.604Z
346+
// - Date {}
344347

345348
assert.deepStrictEqual(NaN, NaN);
346349
// OK, because of the SameValue comparison
347350

348351
// Different unwrapped numbers:
349352
assert.deepStrictEqual(new Number(1), new Number(2));
350-
// AssertionError: Input A expected to strictly deep-equal input B:
351-
// + expected - actual
352-
// - [Number: 1]
353-
// + [Number: 2]
353+
// AssertionError: Expected inputs to be strictly deep-equal:
354+
// + actual - expected
355+
//
356+
// + [Number: 1]
357+
// - [Number: 2]
354358

355359
assert.deepStrictEqual(new String('foo'), Object('foo'));
356360
// OK because the object and the string are identical when unwrapped.
@@ -360,17 +364,20 @@ assert.deepStrictEqual(-0, -0);
360364

361365
// Different zeros using the SameValue Comparison:
362366
assert.deepStrictEqual(0, -0);
363-
// AssertionError: Input A expected to strictly deep-equal input B:
364-
// + expected - actual
365-
// - 0
366-
// + -0
367+
// AssertionError: Expected inputs to be strictly deep-equal:
368+
// + actual - expected
369+
//
370+
// + 0
371+
// - -0
367372

368373
const symbol1 = Symbol();
369374
const symbol2 = Symbol();
370375
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
371376
// OK, because it is the same symbol on both objects.
377+
372378
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
373-
// AssertionError [ERR_ASSERTION]: Input objects not identical:
379+
// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
380+
//
374381
// {
375382
// [Symbol()]: 1
376383
// }
@@ -385,12 +392,13 @@ assert.deepStrictEqual(weakMap1, weakMap2);
385392

386393
// Fails because weakMap3 has a property that weakMap1 does not contain:
387394
assert.deepStrictEqual(weakMap1, weakMap3);
388-
// AssertionError: Input A expected to strictly deep-equal input B:
389-
// + expected - actual
395+
// AssertionError: Expected inputs to be strictly deep-equal:
396+
// + actual - expected
397+
//
390398
// WeakMap {
391-
// - [items unknown]
392-
// + [items unknown],
393-
// + unequal: true
399+
// + [items unknown]
400+
// - [items unknown],
401+
// - unequal: true
394402
// }
395403
```
396404

@@ -875,7 +883,9 @@ assert.notStrictEqual(1, 2);
875883
// OK
876884

877885
assert.notStrictEqual(1, 1);
878-
// AssertionError [ERR_ASSERTION]: Identical input passed to notStrictEqual: 1
886+
// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:
887+
//
888+
// 1
879889

880890
assert.notStrictEqual(1, '1');
881891
// OK
@@ -1031,19 +1041,20 @@ determined by the [SameValue Comparison][].
10311041
const assert = require('assert').strict;
10321042

10331043
assert.strictEqual(1, 2);
1034-
// AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
1035-
// + expected - actual
1036-
// - 1
1037-
// + 2
1044+
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
1045+
//
1046+
// 1 !== 2
10381047

10391048
assert.strictEqual(1, 1);
10401049
// OK
10411050

1042-
assert.strictEqual(1, '1');
1043-
// AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
1044-
// + expected - actual
1045-
// - 1
1046-
// + '1'
1051+
assert.strictEqual('Hello foobar', 'Hello World!');
1052+
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
1053+
// + actual - expected
1054+
//
1055+
// + 'Hello foobar'
1056+
// - 'Hello World!'
1057+
// ^
10471058
```
10481059

10491060
If the values are not strictly equal, an `AssertionError` is thrown with a
@@ -1211,20 +1222,21 @@ function notThrowing() {}
12111222
assert.throws(throwingFirst, 'Second');
12121223
// In the next example the message has no benefit over the message from the
12131224
// error and since it is not clear if the user intended to actually match
1214-
// against the error message, Node.js thrown an `ERR_AMBIGUOUS_ARGUMENT` error.
1225+
// against the error message, Node.js throws an `ERR_AMBIGUOUS_ARGUMENT` error.
12151226
assert.throws(throwingSecond, 'Second');
1216-
// Throws an error:
12171227
// TypeError [ERR_AMBIGUOUS_ARGUMENT]
12181228

12191229
// The string is only used (as message) in case the function does not throw:
12201230
assert.throws(notThrowing, 'Second');
12211231
// AssertionError [ERR_ASSERTION]: Missing expected exception: Second
12221232

12231233
// If it was intended to match for the error message do this instead:
1234+
// It does not throw because the error messages match.
12241235
assert.throws(throwingSecond, /Second$/);
1225-
// Does not throw because the error messages match.
1236+
1237+
// If the error message does not match, the error from within the function is
1238+
// not caught.
12261239
assert.throws(throwingFirst, /Second$/);
1227-
// Throws an error:
12281240
// Error: First
12291241
// at throwingFirst (repl:2:9)
12301242
```

0 commit comments

Comments
 (0)