@@ -102,31 +102,25 @@ It can be accessed using:
102
102
const assert = require (' assert' ).strict ;
103
103
```
104
104
105
- Example error diff (the ` expected ` , ` actual ` , and ` Lines skipped ` will be on a
106
- single row):
105
+ Example error diff:
107
106
108
107
``` js
109
108
const assert = require (' assert' ).strict ;
110
109
111
110
assert .deepEqual ([[[1 , 2 , 3 ]], 4 , 5 ], [[[1 , 2 , ' 3' ]], 4 , 5 ]);
112
- ```
113
-
114
- ``` diff
115
- AssertionError [ERR_ASSERTION]: Input A expected to deepStrictEqual input B:
116
- + expected
117
- - actual
118
- ... Lines skipped
119
-
120
- [
121
- [
122
- ...
123
- 2,
124
- - 3
125
- + '3'
126
- ],
127
- ...
128
- 5
129
- ]
111
+ // AssertionError: Input A expected to strictly deep-equal input B:
112
+ // + expected - actual ... Lines skipped
113
+ //
114
+ // [
115
+ // [
116
+ // ...
117
+ // 2,
118
+ // - 3
119
+ // + '3'
120
+ // ],
121
+ // ...
122
+ // 5
123
+ // ]
130
124
```
131
125
132
126
To deactivate the colors, use the ` NODE_DISABLE_COLORS ` environment variable.
@@ -319,43 +313,67 @@ are recursively evaluated also by the following rules.
319
313
``` js
320
314
const assert = require (' assert' ).strict ;
321
315
316
+ // This fails because 1 !== '1'.
322
317
assert .deepStrictEqual ({ a: 1 }, { a: ' 1' });
323
- // AssertionError: { a: 1 } deepStrictEqual { a: '1' }
324
- // because 1 !== '1' using SameValue comparison
318
+ // AssertionError: Input A expected to strictly deep-equal input B:
319
+ // + expected - actual
320
+ // {
321
+ // - a: 1
322
+ // + a: '1'
323
+ // }
325
324
326
325
// The following objects don't have own properties
327
326
const date = new Date ();
328
327
const object = {};
329
328
const fakeDate = {};
330
329
Object .setPrototypeOf (fakeDate, Date .prototype );
331
330
331
+ // Different [[Prototype]]:
332
332
assert .deepStrictEqual (object, fakeDate);
333
- // AssertionError: {} deepStrictEqual Date {}
334
- // Different [[Prototype]]
333
+ // AssertionError: Input A expected to strictly deep-equal input B:
334
+ // + expected - actual
335
+ // - {}
336
+ // + Date {}
335
337
338
+ // Different type tags:
336
339
assert .deepStrictEqual (date, fakeDate);
337
- // AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {}
338
- // Different type tags
340
+ // AssertionError: Input A expected to strictly deep-equal input B:
341
+ // + expected - actual
342
+ // - 2018-04-26T00:49:08.604Z
343
+ // + Date {}
339
344
340
345
assert .deepStrictEqual (NaN , NaN );
341
346
// OK, because of the SameValue comparison
342
347
348
+ // Different unwrapped numbers:
343
349
assert .deepStrictEqual (new Number (1 ), new Number (2 ));
344
- // Fails because the wrapped number is unwrapped and compared as well.
350
+ // AssertionError: Input A expected to strictly deep-equal input B:
351
+ // + expected - actual
352
+ // - [Number: 1]
353
+ // + [Number: 2]
354
+
345
355
assert .deepStrictEqual (new String (' foo' ), Object (' foo' ));
346
356
// OK because the object and the string are identical when unwrapped.
347
357
348
358
assert .deepStrictEqual (- 0 , - 0 );
349
359
// OK
360
+
361
+ // Different zeros using the SameValue Comparison:
350
362
assert .deepStrictEqual (0 , - 0 );
351
- // AssertionError: 0 deepStrictEqual -0
363
+ // AssertionError: Input A expected to strictly deep-equal input B:
364
+ // + expected - actual
365
+ // - 0
366
+ // + -0
352
367
353
368
const symbol1 = Symbol ();
354
369
const symbol2 = Symbol ();
355
370
assert .deepStrictEqual ({ [symbol1]: 1 }, { [symbol1]: 1 });
356
371
// OK, because it is the same symbol on both objects.
357
372
assert .deepStrictEqual ({ [symbol1]: 1 }, { [symbol2]: 1 });
358
- // Fails because symbol1 !== symbol2!
373
+ // AssertionError [ERR_ASSERTION]: Input objects not identical:
374
+ // {
375
+ // [Symbol()]: 1
376
+ // }
359
377
360
378
const weakMap1 = new WeakMap ();
361
379
const weakMap2 = new WeakMap ([[{}, {}]]);
@@ -364,8 +382,16 @@ weakMap3.unequal = true;
364
382
365
383
assert .deepStrictEqual (weakMap1, weakMap2);
366
384
// OK, because it is impossible to compare the entries
385
+
386
+ // Fails because weakMap3 has a property that weakMap1 does not contain:
367
387
assert .deepStrictEqual (weakMap1, weakMap3);
368
- // Fails because weakMap3 has a property that weakMap1 does not contain!
388
+ // AssertionError: Input A expected to strictly deep-equal input B:
389
+ // + expected - actual
390
+ // WeakMap {
391
+ // - [items unknown]
392
+ // + [items unknown],
393
+ // + unequal: true
394
+ // }
369
395
```
370
396
371
397
If the values are not equal, an ` AssertionError ` is thrown with a ` message `
@@ -639,7 +665,9 @@ changes:
639
665
* ` value ` {any}
640
666
641
667
Throws ` value ` if ` value ` is not ` undefined ` or ` null ` . This is useful when
642
- testing the ` error ` argument in callbacks.
668
+ testing the ` error ` argument in callbacks. The stack trace contains all frames
669
+ from the error passed to ` ifError() ` including the potential new frames for
670
+ ` ifError() ` itself. See below for an example.
643
671
644
672
``` js
645
673
const assert = require (' assert' ).strict ;
@@ -652,6 +680,19 @@ assert.ifError('error');
652
680
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
653
681
assert .ifError (new Error ());
654
682
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
683
+
684
+ // Create some random error frames.
685
+ let err;
686
+ (function errorFrame () {
687
+ err = new Error (' test error' );
688
+ })();
689
+
690
+ (function ifErrorFrame () {
691
+ assert .ifError (err);
692
+ })();
693
+ // AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error
694
+ // at ifErrorFrame
695
+ // at errorFrame
655
696
```
656
697
657
698
## assert.notDeepEqual(actual, expected[ , message] )
@@ -834,7 +875,7 @@ assert.notStrictEqual(1, 2);
834
875
// OK
835
876
836
877
assert .notStrictEqual (1 , 1 );
837
- // AssertionError: 1 notStrictEqual 1
878
+ // AssertionError [ERR_ASSERTION]: Identical input passed to notStrictEqual: 1
838
879
839
880
assert .notStrictEqual (1 , ' 1' );
840
881
// OK
@@ -880,40 +921,34 @@ assert.ok(1);
880
921
// OK
881
922
882
923
assert .ok ();
883
- // throws:
884
- // "AssertionError: No value argument passed to `assert.ok`.
924
+ // AssertionError: No value argument passed to `assert.ok()`
885
925
886
926
assert .ok (false , ' it\' s false' );
887
- // throws " AssertionError: it's false"
927
+ // AssertionError: it's false
888
928
889
929
// In the repl:
890
930
assert .ok (typeof 123 === ' string' );
891
- // throws:
892
- // "AssertionError: false == true
931
+ // AssertionError: false == true
893
932
894
933
// In a file (e.g. test.js):
895
934
assert .ok (typeof 123 === ' string' );
896
- // throws:
897
- // "AssertionError: The expression evaluated to a falsy value:
935
+ // AssertionError: The expression evaluated to a falsy value:
898
936
//
899
937
// assert.ok(typeof 123 === 'string')
900
938
901
939
assert .ok (false );
902
- // throws:
903
- // "AssertionError: The expression evaluated to a falsy value:
940
+ // AssertionError: The expression evaluated to a falsy value:
904
941
//
905
942
// assert.ok(false)
906
943
907
944
assert .ok (0 );
908
- // throws:
909
- // "AssertionError: The expression evaluated to a falsy value:
945
+ // AssertionError: The expression evaluated to a falsy value:
910
946
//
911
947
// assert.ok(0)
912
948
913
949
// Using `assert()` works the same:
914
950
assert (0 );
915
- // throws:
916
- // "AssertionError: The expression evaluated to a falsy value:
951
+ // AssertionError: The expression evaluated to a falsy value:
917
952
//
918
953
// assert(0)
919
954
```
@@ -995,13 +1030,19 @@ determined by the [SameValue Comparison][].
995
1030
const assert = require (' assert' ).strict ;
996
1031
997
1032
assert .strictEqual (1 , 2 );
998
- // AssertionError: 1 strictEqual 2
1033
+ // AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
1034
+ // + expected - actual
1035
+ // - 1
1036
+ // + 2
999
1037
1000
1038
assert .strictEqual (1 , 1 );
1001
1039
// OK
1002
1040
1003
1041
assert .strictEqual (1 , ' 1' );
1004
- // AssertionError: 1 strictEqual '1'
1042
+ // AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
1043
+ // + expected - actual
1044
+ // - 1
1045
+ // + '1'
1005
1046
```
1006
1047
1007
1048
If the values are not strictly equal, an ` AssertionError ` is thrown with a
@@ -1035,6 +1076,34 @@ each property will be tested for including the non-enumerable `message` and
1035
1076
If specified, ` message ` will be the message provided by the ` AssertionError ` if
1036
1077
the block fails to throw.
1037
1078
1079
+ Custom error object / error instance:
1080
+
1081
+ ``` js
1082
+ const err = new TypeError (' Wrong value' );
1083
+ err .code = 404 ;
1084
+
1085
+ assert .throws (
1086
+ () => {
1087
+ throw err;
1088
+ },
1089
+ {
1090
+ name: ' TypeError' ,
1091
+ message: ' Wrong value'
1092
+ // Note that only properties on the error object will be tested!
1093
+ }
1094
+ );
1095
+
1096
+ // Fails due to the different `message` and `name` properties:
1097
+ assert .throws (
1098
+ () => {
1099
+ const otherErr = new Error (' Not found' );
1100
+ otherErr .code = 404 ;
1101
+ throw otherErr;
1102
+ },
1103
+ err // This tests for `message`, `name` and `code`.
1104
+ );
1105
+ ```
1106
+
1038
1107
Validate instanceof using constructor:
1039
1108
1040
1109
``` js
@@ -1076,39 +1145,12 @@ assert.throws(
1076
1145
);
1077
1146
```
1078
1147
1079
- Custom error object / error instance:
1080
-
1081
- ``` js
1082
- const err = new TypeError (' Wrong value' );
1083
- err .code = 404 ;
1084
-
1085
- assert .throws (
1086
- () => {
1087
- throw err;
1088
- },
1089
- {
1090
- name: ' TypeError' ,
1091
- message: ' Wrong value'
1092
- // Note that only properties on the error object will be tested!
1093
- }
1094
- );
1095
-
1096
- // Fails due to the different `message` and `name` properties:
1097
- assert .throws (
1098
- () => {
1099
- const otherErr = new Error (' Not found' );
1100
- otherErr .code = 404 ;
1101
- throw otherErr;
1102
- },
1103
- err // This tests for `message`, `name` and `code`.
1104
- );
1105
- ```
1106
-
1107
1148
Note that ` error ` cannot be a string. If a string is provided as the second
1108
1149
argument, then ` error ` is assumed to be omitted and the string will be used for
1109
- ` message ` instead. This can lead to easy-to-miss mistakes. Please read the
1110
- example below carefully if using a string as the second argument gets
1111
- considered:
1150
+ ` message ` instead. This can lead to easy-to-miss mistakes. Using the same
1151
+ message as the thrown error message is going to result in an
1152
+ ` ERR_AMBIGUOUS_ARGUMENT ` error. Please read the example below carefully if using
1153
+ a string as the second argument gets considered:
1112
1154
1113
1155
<!-- eslint-disable no-restricted-syntax -->
1114
1156
``` js
@@ -1121,10 +1163,15 @@ function throwingSecond() {
1121
1163
function notThrowing () {}
1122
1164
1123
1165
// The second argument is a string and the input function threw an Error.
1124
- // In that case both cases do not throw as neither is going to try to
1125
- // match for the error message thrown by the input function!
1166
+ // The first case will not throw as it does not match for the error message
1167
+ // thrown by the input function!
1126
1168
assert .throws (throwingFirst, ' Second' );
1169
+ // In the next example the message has no benefit over the message from the
1170
+ // error and since it is not clear if the user intended to actually match
1171
+ // against the error message, Node.js thrown an `ERR_AMBIGUOUS_ARGUMENT` error.
1127
1172
assert .throws (throwingSecond, ' Second' );
1173
+ // Throws an error:
1174
+ // TypeError [ERR_AMBIGUOUS_ARGUMENT]
1128
1175
1129
1176
// The string is only used (as message) in case the function does not throw:
1130
1177
assert .throws (notThrowing, ' Second' );
@@ -1134,7 +1181,7 @@ assert.throws(notThrowing, 'Second');
1134
1181
assert .throws (throwingSecond, / Second$ / );
1135
1182
// Does not throw because the error messages match.
1136
1183
assert .throws (throwingFirst, / Second$ / );
1137
- // Throws a error:
1184
+ // Throws an error:
1138
1185
// Error: First
1139
1186
// at throwingFirst (repl:2:9)
1140
1187
```
0 commit comments