8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7293c58 commit 90026c3Copy full SHA for 90026c3
doc/api/assert.md
@@ -102,31 +102,25 @@ It can be accessed using:
102
const assert = require('assert').strict;
103
```
104
105
-Example error diff (the `expected`, `actual`, and `Lines skipped` will be on a
106
-single row):
+Example error diff:
107
108
```js
109
110
111
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
- ]
+// AssertionError: Input A expected to strictly deep-equal input B:
+// + expected - actual ... Lines skipped
+//
+// [
+// ...
+// 2,
+// - 3
+// + '3'
+// ],
+// 5
+// ]
130
131
132
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
320
314
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
+// + expected - actual
+// {
+// - a: 1
+// + a: '1'
+// }
325
326
// The following objects don't have own properties
327
const date = new Date();
328
const object = {};
329
const fakeDate = {};
330
Object.setPrototypeOf(fakeDate, Date.prototype);
331
+// Different [[Prototype]]:
332
assert.deepStrictEqual(object, fakeDate);
333
-// AssertionError: {} deepStrictEqual Date {}
334
-// Different [[Prototype]]
335
+// - {}
336
+// + Date {}
337
338
+// Different type tags:
339
assert.deepStrictEqual(date, fakeDate);
-// AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {}
-// Different type tags
340
341
342
+// - 2018-04-26T00:49:08.604Z
343
344
345
assert.deepStrictEqual(NaN, NaN);
346
// OK, because of the SameValue comparison
347
348
+// Different unwrapped numbers:
349
assert.deepStrictEqual(new Number(1), new Number(2));
-// Fails because the wrapped number is unwrapped and compared as well.
350
351
352
+// - [Number: 1]
353
+// + [Number: 2]
354
+
355
assert.deepStrictEqual(new String('foo'), Object('foo'));
356
// OK because the object and the string are identical when unwrapped.
357
358
assert.deepStrictEqual(-0, -0);
359
// OK
360
361
+// Different zeros using the SameValue Comparison:
362
assert.deepStrictEqual(0, -0);
-// AssertionError: 0 deepStrictEqual -0
363
364
365
+// - 0
366
+// + -0
367
368
const symbol1 = Symbol();
369
const symbol2 = Symbol();
370
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
371
// OK, because it is the same symbol on both objects.
372
assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
-// Fails because symbol1 !== symbol2!
373
+// AssertionError [ERR_ASSERTION]: Input objects not identical:
374
375
+// [Symbol()]: 1
376
377
378
const weakMap1 = new WeakMap();
379
const weakMap2 = new WeakMap([[{}, {}]]);
@@ -364,8 +382,16 @@ weakMap3.unequal = true;
382
383
assert.deepStrictEqual(weakMap1, weakMap2);
384
// OK, because it is impossible to compare the entries
385
386
+// Fails because weakMap3 has a property that weakMap1 does not contain:
387
assert.deepStrictEqual(weakMap1, weakMap3);
-// Fails because weakMap3 has a property that weakMap1 does not contain!
388
389
390
+// WeakMap {
391
+// - [items unknown]
392
+// + [items unknown],
393
+// + unequal: true
394
395
396
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
645
673
@@ -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
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
@@ -880,40 +921,34 @@ assert.ok(1);
921
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
892
-// "AssertionError: false == true
931
+// AssertionError: false == true
893
932
894
933
// In a file (e.g. test.js):
895
934
896
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
903
940
904
941
905
942
// assert.ok(false)
906
943
907
944
assert.ok(0);
908
909
945
910
946
911
947
// assert.ok(0)
912
948
913
949
// Using `assert()` works the same:
914
950
assert(0);
915
916
951
917
952
918
953
// assert(0)
919
954
@@ -995,13 +1030,19 @@ determined by the [SameValue Comparison][].
995
1030
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
1035
+// - 1
1036
+// + 2
999
1037
1000
1038
assert.strictEqual(1, 1);
1001
1039
1002
1040
1003
1041
assert.strictEqual(1, '1');
1004
-// AssertionError: 1 strictEqual '1'
1042
1043
1044
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
1076
If specified, `message` will be the message provided by the `AssertionError` if
1077
the block fails to throw.
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
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
Validate instanceof using constructor:
1108
1109
@@ -1076,39 +1145,12 @@ assert.throws(
1145
);
1146
1147
-Custom error object / error instance:
-```js
-const err = new TypeError('Wrong value');
-err.code = 404;
-assert.throws(
- () => {
- throw err;
- },
- {
- name: 'TypeError',
- message: 'Wrong value'
- // Note that only properties on the error object will be tested!
- }
-);
-// Fails due to the different `message` and `name` properties:
- const otherErr = new Error('Not found');
- otherErr.code = 404;
- throw otherErr;
- err // This tests for `message`, `name` and `code`.
1148
Note that `error` cannot be a string. If a string is provided as the second
1149
argument, then `error` is assumed to be omitted and the string will be used for
-`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
@@ -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
1138
1185
// Error: First
1139
1186
// at throwingFirst (repl:2:9)
1140
1187