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 761af37 commit 823269dCopy full SHA for 823269d
doc/api/assert.markdown
@@ -2,42 +2,131 @@
2
3
Stability: 3 - Locked
4
5
-This module is used so that Node.js can test itself. It can be accessed with
6
-`require('assert')`. However, it is recommended that a userland assertion
7
-library be used instead.
+The `assert` module provides a simple set of assertion tests that can be used
+to test invariants and implement unit tests. While the `assert` module is
+generally intended for internal use by Node.js itself, it can be used by user
8
+code calling `require('assert')`.
9
+
10
+The API for the `assert` module is [Locked][]. This means that there will be no
11
+additions or changes to any of the methods implemented and exposed by
12
+the module.
13
14
## assert(value[, message]), assert.ok(value[, message])
15
-Tests if value is truthy. It is equivalent to
16
+Tests if `value` is truthy. It is equivalent to
17
`assert.equal(!!value, true, message)`.
18
19
+If `value` is not truthy, an `AssertionError` is thrown with a `message`
20
+property set equal to the value of the `message` parameter. If the `message`
21
+parameter is `undefined`, a default error message is assigned.
22
23
+ const assert = require('assert');
24
25
+ assert(true); // OK
26
+ assert(1); // OK
27
+ assert(false);
28
+ // throws "AssertionError: false == true"
29
+ assert(0);
30
+ // throws "AssertionError: 0 == true"
31
+ assert(false, 'it\'s false');
32
+ // throws "AssertionError: it's false"
33
34
+ assert.ok(true); // OK
35
+ assert.ok(1); // OK
36
+ assert.ok(false);
37
38
+ assert.ok(0);
39
40
+ assert.ok(false, 'it\'s false');
41
42
43
## assert.deepEqual(actual, expected[, message])
44
-Tests for deep equality. Primitive values are compared with the equal
-comparison operator ( `==` ).
45
+Tests for deep equality between the `actual` and `expected` parameters.
46
+Primitive values are compared with the equal comparison operator ( `==` ).
47
-This only considers enumerable properties. It does not test object prototypes,
-attached symbols, or non-enumerable properties. This can lead to some
-potentially surprising results. For example, this does not throw an
-`AssertionError` because the properties on the [`Error`][] object are
-non-enumerable:
48
+Only enumerable "own" properties are considered. The `deepEqual()`
49
+implementation does not test object prototypes, attached symbols, or
50
+non-enumerable properties. This can lead to some potentially surprising
51
+results. For example, the following example does not throw an `AssertionError`
52
+because the properties on the [`Error`][] object are non-enumerable:
53
54
// WARNING: This does not throw an AssertionError!
55
assert.deepEqual(Error('a'), Error('b'));
56
57
+"Deep" equality means that the enumerable "own" properties of child objects
58
+are evaluated also:
59
60
61
62
+ const obj1 = {
63
+ a : {
64
+ b : 1
65
+ }
66
+ };
67
+ const obj2 = {
68
69
+ b : 2
70
71
72
+ const obj3 = {
73
74
75
76
77
+ const obj4 = Object.create(obj1);
78
79
+ assert.deepEqual( 9E88 obj1, obj1);
80
+ // OK, object is equal to itself
81
82
+ assert.deepEqual(obj1, obj2);
83
+ // AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
84
+ // values of b are different
85
86
+ assert.deepEqual(obj1, obj3);
87
+ // OK, objects are equal
88
89
+ assert.deepEqual(obj1, obj4);
90
+ // AssertionError: { a: { b: 1 } } deepEqual {}
91
+ // Prototypes are ignored
92
93
+If the values are not equal, an `AssertionError` is thrown with a `message`
94
95
+parameter is undefined, a default error message is assigned.
96
97
## assert.deepStrictEqual(actual, expected[, message])
98
-Tests for deep equality. Primitive values are compared with the strict equality
-operator ( `===` ).
99
+Generally identical to `assert.deepEqual` with the exception that primitive
100
+values are compared using the strict equality operator ( `===` ).
101
102
103
104
+ assert.deepEqual({a:1}, {a:'1'});
105
+ // OK, because 1 == '1'
106
107
+ assert.deepStrictEqual({a:1}, {a:'1'});
108
+ // AssertionError: { a: 1 } deepStrictEqual { a: '1' }
109
+ // because 1 !== '1' using strict equality
110
111
112
113
114
115
## assert.doesNotThrow(block[, error][, message])
116
-Expects `block` not to throw an error. See [`assert.throws()`][] for more details.
117
+Asserts that the function `block` does not throw an error. See
118
+[`assert.throws()`][] for more details.
119
120
+When `assert.doesNotThrow()` is called, it will immediately call the `block`
121
+function.
122
123
+If an error is thrown and it is the same type as that specified by the `error`
124
+parameter, then an `AssertionError` is thrown. If the error is of a different
125
+type, or if the `error` parameter is undefined, the error is propagated back
126
+to the caller.
127
-If `block` throws an error and if it is of a different type from `error`, the
-thrown error will get propagated back to the caller. The following call will
-throw the [`TypeError`][], since we're not matching the error types in the
-assertion.
128
+The following, for instance, will throw the [`TypeError`][] because there is no
129
+matching error type in the assertion:
130
131
assert.doesNotThrow(
132
function() {
@@ -46,8 +135,8 @@ assertion.
135
SyntaxError
136
);
137
-In case `error` matches with the error thrown by `block`, an `AssertionError`
-is thrown instead.
138
+However, the following will result in an `AssertionError` with the message
139
+'Got unwanted exception (TypeError)..':
140
141
142
@@ -56,47 +145,184 @@ is thrown instead.
145
TypeError
146
147
148
+If an `AssertionError` is thrown and a value is provided for the `message`
149
+parameter, the value of `message` will be appended to the `AssertionError`
150
+message:
151
152
+ assert.doesNotThrow(
153
+ function() {
154
+ throw new TypeError('Wrong value');
155
+ },
156
+ TypeError,
157
+ 'Whoops'
158
+ );
159
+ // Throws: AssertionError: Got unwanted exception (TypeError). Whoops
160
161
## assert.equal(actual, expected[, message])
162
-Tests shallow, coercive equality with the equal comparison operator ( `==` ).
163
+Tests shallow, coercive equality between the `actual` and `expected` parameters
164
+using the equal comparison operator ( `==` ).
165
166
167
168
+ assert.equal(1, 1);
169
+ // OK, 1 == 1
170
+ assert.equal(1, '1');
171
+ // OK, 1 == '1'
172
173
+ assert.equal(1, 2);
174
+ // AssertionError: 1 == 2
175
+ assert.equal({a: {b: 1}}, {a: {b: 1}});
176
+ //AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
177
178
179
180
181
182
## assert.fail(actual, expected, message, operator)
183
-Throws an `AssertionError`. If `message` is falsy, it displays the values for
-`actual` and `expected` separated by the provided `operator`. Otherwise, it
-displays `message` (and does not use `actual`, `expected`, and `operator`).
184
+Throws an `AssertionError`. If `message` is falsy, the error message is set as
185
+the values of `actual` and `expected` separated by the provided `operator`.
186
+Otherwise, the error message is the value of `message`.
187
188
189
190
+ assert.fail(1, 2, undefined, '>');
191
+ // AssertionError: 1 > 2
192
193
+ assert.fail(1, 2, 'whoops', '>');
194
+ // AssertionError: whoops
195
196
## assert.ifError(value)
197
198
Throws `value` if `value` is truthy. This is useful when testing the `error`
199
argument in callbacks.
200
201
202
203
+ assert.ifError(0); // OK
204
+ assert.ifError(1); // Throws 1
205
+ assert.ifError('error') // Throws 'error'
206
+ assert.ifError(new Error()); // Throws Error
207
208
## assert.notDeepEqual(actual, expected[, message])
209
210
Tests for any deep inequality. Opposite of [`assert.deepEqual`][].
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
+ assert.deepEqual(obj1, obj1);
232
+ AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
233
234
235
+ // OK, obj1 and obj2 are not deeply equal
236
237
238
+ // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
239
240
241
242
243
+If the values are deeply equal, an `AssertionError` is thrown with a `message`
244
245
246
247
## assert.notDeepStrictEqual(actual, expected[, message])
248
-Tests for deep inequality. Opposite of [`assert.deepStrictEqual`][].
249
+Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual`][].
250
251
252
253
+ assert.notDeepEqual({a:1}, {a:'1'});
254
+ // AssertionError: { a: 1 } notDeepEqual { a: '1' }
255
256
+ assert.notDeepStrictEqual({a:1}, {a:'1'});
257
+ // OK
258
259
+If the values are deeply and strictly equal, an `AssertionError` is thrown
260
+with a `message` property set equal to the value of the `message` parameter. If
261
+the `message` parameter is undefined, a default error message is assigned.
262
263
## assert.notEqual(actual, expected[, message])
264
265
Tests shallow, coercive inequality with the not equal comparison operator
266
( `!=` ).
267
268
269
270
+ assert.notEqual(1, 2);
271
272
273
+ assert.notEqual(1, 1);
274
+ // AssertionError: 1 != 1
275
276
+ assert.notEqual(1, '1');
277
+ // AssertionError: 1 != '1'
278
279
+If the values are equal, an `AssertionError` is thrown with a `message`
280
281
282
283
## assert.notStrictEqual(actual, expected[, message])
284
285
Tests strict inequality as determined by the strict not equal operator
286
( `!==` ).
287
288
289
290
+ assert.notStrictEqual(1, 2);
291
292
293
+ assert.notStrictEqual(1, 1);
294
295
296
+ assert.notStrictEqual(1, '1');
297
298
299
+If the values are strictly equal, an `AssertionError` is thrown with a
300
+`message` property set equal to the value of the `message` parameter. If the
301
+`message` parameter is undefined, a default error message is assigned.
302
303
## assert.strictEqual(actual, expected[, message])
304
305
Tests strict equality as determined by the strict equality operator ( `===` ).
306
307
308
309
+ assert.strictEqual(1, 2);
310
+ // AssertionError: 1 === 2
311
312
+ assert.strictEqual(1, 1);
313
314
315
+ assert.strictEqual(1, '1');
316
+ // AssertionError: 1 === '1'
317
318
+If the values are not strictly equal, an `AssertionError` is thrown with a
319
320
321
322
## assert.throws(block[, error][, message])
323
-Expects `block` to 38BA throw an error. `error` can be a constructor, [`RegExp`][], or
-validation function.
324
+Expects the function `block` to throw an error. If specified, `error` can be a
325
+constructor, [`RegExp`][], or validation function.
326
327
Validate instanceof using constructor:
328
@@ -130,6 +356,7 @@ Custom error validation:
356
'unexpected error'
357
358
359
+[Locked]: documentation.html#documentation_stability_index
133
360
[`assert.deepEqual`]: #assert_assert_deepequal_actual_expected_message
134
361
[`assert.deepStrictEqual`]: #assert_assert_deepstrictequal_actual_expected_message
362
[`assert.throws()`]: #assert_assert_throws_block_error_message