Skip to content

Latest commit

Β 

History

History
608 lines (493 loc) Β· 15.7 KB

assert.md

File metadata and controls

608 lines (493 loc) Β· 15.7 KB
Β 
Feb 29, 2012
Feb 29, 2012
1
# Assert
Oct 28, 2010
Oct 28, 2010
2
Feb 15, 2017
Feb 15, 2017
3
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
4
Jan 21, 2016
Jan 21, 2016
5
The `assert` module provides a simple set of assertion tests that can be used to
Feb 3, 2017
Feb 3, 2017
6
test invariants.
Dec 28, 2015
Dec 28, 2015
7
Jan 30, 2016
Jan 30, 2016
8
## assert(value[, message])
May 13, 2016
May 13, 2016
9
10
11
<!-- YAML
added: v0.5.9
-->
Feb 27, 2017
Feb 27, 2017
12
13
* `value` {any}
* `message` {any}
Oct 28, 2010
Oct 28, 2010
14
Mar 22, 2017
Mar 22, 2017
15
An alias of [`assert.ok()`][].
Dec 28, 2015
Dec 28, 2015
16
Sep 25, 2014
Sep 25, 2014
17
## assert.deepEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
18
19
<!-- YAML
added: v0.1.21
Feb 24, 2017
Feb 24, 2017
20
changes:
Apr 3, 2017
Apr 3, 2017
21
22
23
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/12142
description: Set and Map content is also compared
Feb 24, 2017
Feb 24, 2017
24
25
26
27
28
29
30
31
32
- version: v6.4.0, v4.7.1
pr-url: https://github.com/nodejs/node/pull/8002
description: Typed array slices are handled correctly now.
- version: v6.1.0, v4.5.0
pr-url: https://github.com/nodejs/node/pull/6432
description: Objects with circular references can be used as inputs now.
- version: v5.10.1, v4.4.3
pr-url: https://github.com/nodejs/node/pull/5910
description: Handle non-`Uint8Array` typed arrays correctly.
May 13, 2016
May 13, 2016
33
-->
Feb 27, 2017
Feb 27, 2017
34
35
36
* `actual` {any}
* `expected` {any}
* `message` {any}
Oct 28, 2010
Oct 28, 2010
37
Dec 28, 2015
Dec 28, 2015
38
Tests for deep equality between the `actual` and `expected` parameters.
Feb 27, 2017
Feb 27, 2017
39
40
41
42
43
44
45
Primitive values are compared with the [Abstract Equality Comparison][]
( `==` ).
Only [enumerable "own" properties][] are considered. The
[`assert.deepEqual()`][] implementation does not test the
[`[[Prototype]]`][prototype-spec] of objects, attached symbols, or
non-enumerable properties β€” for such checks, consider using
Apr 3, 2017
Apr 3, 2017
46
[`assert.deepStrictEqual()`][] instead. This can lead to some
Feb 27, 2017
Feb 27, 2017
47
48
49
potentially surprising results. For example, the following example does not
throw an `AssertionError` because the properties on the [`Error`][] object are
not enumerable:
Oct 19, 2015
Oct 19, 2015
50
Jan 21, 2016
Jan 21, 2016
51
52
53
54
```js
// WARNING: This does not throw an AssertionError!
assert.deepEqual(Error('a'), Error('b'));
```
Oct 28, 2010
Oct 28, 2010
55
Apr 3, 2017
Apr 3, 2017
56
57
58
An exception is made for [`Map`][] and [`Set`][]. Maps and Sets have their
contained items compared too, as expected.
Dec 28, 2015
Dec 28, 2015
59
60
61
"Deep" equality means that the enumerable "own" properties of child objects
are evaluated also:
Jan 21, 2016
Jan 21, 2016
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
```js
const assert = require('assert');
const obj1 = {
a : {
b : 1
}
};
const obj2 = {
a : {
b : 2
}
};
const obj3 = {
a : {
b : 1
}
Jul 15, 2016
Jul 15, 2016
79
};
Jan 21, 2016
Jan 21, 2016
80
81
82
const obj4 = Object.create(obj1);
assert.deepEqual(obj1, obj1);
Nov 16, 2016
Nov 16, 2016
83
// OK, object is equal to itself
Jan 21, 2016
Jan 21, 2016
84
85
assert.deepEqual(obj1, obj2);
Nov 16, 2016
Nov 16, 2016
86
87
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
// values of b are different
Jan 21, 2016
Jan 21, 2016
88
89
assert.deepEqual(obj1, obj3);
Nov 16, 2016
Nov 16, 2016
90
// OK, objects are equal
Jan 21, 2016
Jan 21, 2016
91
92
assert.deepEqual(obj1, obj4);
Nov 16, 2016
Nov 16, 2016
93
94
// AssertionError: { a: { b: 1 } } deepEqual {}
// Prototypes are ignored
Jan 21, 2016
Jan 21, 2016
95
```
Dec 28, 2015
Dec 28, 2015
96
97
98
99
100
If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned.
Nov 13, 2015
Nov 13, 2015
101
## assert.deepStrictEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
102
103
<!-- YAML
added: v1.2.0
Feb 24, 2017
Feb 24, 2017
104
changes:
Apr 3, 2017
Apr 3, 2017
105
106
107
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/12142
description: Set and Map content is also compared
Feb 24, 2017
Feb 24, 2017
108
109
110
111
112
113
114
115
116
- version: v6.4.0, v4.7.1
pr-url: https://github.com/nodejs/node/pull/8002
description: Typed array slices are handled correctly now.
- version: v6.1.0
pr-url: https://github.com/nodejs/node/pull/6432
description: Objects with circular references can be used as inputs now.
- version: v5.10.1, v4.4.3
pr-url: https://github.com/nodejs/node/pull/5910
description: Handle non-`Uint8Array` typed arrays correctly.
May 13, 2016
May 13, 2016
117
-->
Feb 27, 2017
Feb 27, 2017
118
119
120
* `actual` {any}
* `expected` {any}
* `message` {any}
Nov 13, 2015
Nov 13, 2015
121
Mar 12, 2017
Mar 12, 2017
122
Generally identical to `assert.deepEqual()` with three exceptions:
Feb 27, 2017
Feb 27, 2017
123
124
1. Primitive values are compared using the [Strict Equality Comparison][]
Apr 3, 2017
Apr 3, 2017
125
126
( `===` ). Set values and Map keys are compared using the [SameValueZero][]
comparison. (Which means they are free of the [caveats][]).
Feb 27, 2017
Feb 27, 2017
127
128
2. [`[[Prototype]]`][prototype-spec] of objects are compared using
the [Strict Equality Comparison][] too.
Mar 12, 2017
Mar 12, 2017
129
3. [Type tags][Object.prototype.toString()] of objects should be the same.
Dec 28, 2015
Dec 28, 2015
130
Jan 21, 2016
Jan 21, 2016
131
132
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
133
Feb 27, 2017
Feb 27, 2017
134
assert.deepEqual({a: 1}, {a: '1'});
Nov 16, 2016
Nov 16, 2016
135
// OK, because 1 == '1'
Dec 28, 2015
Dec 28, 2015
136
Feb 27, 2017
Feb 27, 2017
137
assert.deepStrictEqual({a: 1}, {a: '1'});
Nov 16, 2016
Nov 16, 2016
138
139
// AssertionError: { a: 1 } deepStrictEqual { a: '1' }
// because 1 !== '1' using strict equality
Mar 12, 2017
Mar 12, 2017
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// The following objects don't have own properties
const date = new Date();
const object = {};
const fakeDate = {};
Object.setPrototypeOf(fakeDate, Date.prototype);
assert.deepEqual(object, fakeDate);
// OK, doesn't check [[Prototype]]
assert.deepStrictEqual(object, fakeDate);
// AssertionError: {} deepStrictEqual Date {}
// Different [[Prototype]]
assert.deepEqual(date, fakeDate);
// OK, doesn't check type tags
assert.deepStrictEqual(date, fakeDate);
// AssertionError: 2017-03-11T14:25:31.849Z deepStrictEqual Date {}
// Different type tags
Jan 21, 2016
Jan 21, 2016
159
```
Dec 28, 2015
Dec 28, 2015
160
161
162
163
If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned.
Nov 13, 2015
Nov 13, 2015
164
165
## assert.doesNotThrow(block[, error][, message])
May 13, 2016
May 13, 2016
166
167
<!-- YAML
added: v0.1.21
Feb 24, 2017
Feb 24, 2017
168
169
170
171
172
173
174
changes:
- version: v5.11.0, v4.4.5
pr-url: https://github.com/nodejs/node/pull/2407
description: The `message` parameter is respected now.
- version: v4.2.0
pr-url: https://github.com/nodejs/node/pull/3276
description: The `error` parameter can now be an arrow function.
May 13, 2016
May 13, 2016
175
-->
Feb 27, 2017
Feb 27, 2017
176
177
178
* `block` {Function}
* `error` {RegExp|Function}
* `message` {any}
Nov 13, 2015
Nov 13, 2015
179
Dec 28, 2015
Dec 28, 2015
180
181
182
183
184
185
186
187
188
189
Asserts that the function `block` does not throw an error. See
[`assert.throws()`][] for more details.
When `assert.doesNotThrow()` is called, it will immediately call the `block`
function.
If an error is thrown and it is the same type as that specified by the `error`
parameter, then an `AssertionError` is thrown. If the error is of a different
type, or if the `error` parameter is undefined, the error is propagated back
to the caller.
Nov 13, 2015
Nov 13, 2015
190
Dec 28, 2015
Dec 28, 2015
191
192
The following, for instance, will throw the [`TypeError`][] because there is no
matching error type in the assertion:
Nov 13, 2015
Nov 13, 2015
193
Jan 21, 2016
Jan 21, 2016
194
195
```js
assert.doesNotThrow(
Jan 27, 2016
Jan 27, 2016
196
() => {
Jan 21, 2016
Jan 21, 2016
197
198
199
200
201
throw new TypeError('Wrong value');
},
SyntaxError
);
```
Nov 13, 2015
Nov 13, 2015
202
Dec 28, 2015
Dec 28, 2015
203
204
However, the following will result in an `AssertionError` with the message
'Got unwanted exception (TypeError)..':
Nov 13, 2015
Nov 13, 2015
205
Jan 21, 2016
Jan 21, 2016
206
207
```js
assert.doesNotThrow(
Jan 27, 2016
Jan 27, 2016
208
() => {
Jan 21, 2016
Jan 21, 2016
209
210
211
212
213
throw new TypeError('Wrong value');
},
TypeError
);
```
Nov 13, 2015
Nov 13, 2015
214
Dec 28, 2015
Dec 28, 2015
215
216
217
218
If an `AssertionError` is thrown and a value is provided for the `message`
parameter, the value of `message` will be appended to the `AssertionError`
message:
Jan 21, 2016
Jan 21, 2016
219
220
```js
assert.doesNotThrow(
Jan 27, 2016
Jan 27, 2016
221
() => {
Jan 21, 2016
Jan 21, 2016
222
223
224
225
226
227
228
throw new TypeError('Wrong value');
},
TypeError,
'Whoops'
);
// Throws: AssertionError: Got unwanted exception (TypeError). Whoops
```
Dec 28, 2015
Dec 28, 2015
229
Nov 13, 2015
Nov 13, 2015
230
## assert.equal(actual, expected[, message])
May 13, 2016
May 13, 2016
231
232
233
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
234
235
236
* `actual` {any}
* `expected` {any}
* `message` {any}
Nov 13, 2015
Nov 13, 2015
237
Dec 28, 2015
Dec 28, 2015
238
Tests shallow, coercive equality between the `actual` and `expected` parameters
Feb 27, 2017
Feb 27, 2017
239
using the [Abstract Equality Comparison][] ( `==` ).
Dec 28, 2015
Dec 28, 2015
240
Jan 21, 2016
Jan 21, 2016
241
242
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
243
Jan 21, 2016
Jan 21, 2016
244
assert.equal(1, 1);
Nov 16, 2016
Nov 16, 2016
245
// OK, 1 == 1
Jan 21, 2016
Jan 21, 2016
246
assert.equal(1, '1');
Nov 16, 2016
Nov 16, 2016
247
// OK, 1 == '1'
Dec 28, 2015
Dec 28, 2015
248
Jan 21, 2016
Jan 21, 2016
249
assert.equal(1, 2);
Nov 16, 2016
Nov 16, 2016
250
// AssertionError: 1 == 2
Jan 21, 2016
Jan 21, 2016
251
assert.equal({a: {b: 1}}, {a: {b: 1}});
Nov 16, 2016
Nov 16, 2016
252
//AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
Jan 21, 2016
Jan 21, 2016
253
```
Dec 28, 2015
Dec 28, 2015
254
255
256
257
If the values are not equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned.
Nov 13, 2015
Nov 13, 2015
258
Apr 12, 2017
Apr 12, 2017
259
## assert.fail(message)
Nov 13, 2015
Nov 13, 2015
260
## assert.fail(actual, expected, message, operator)
May 13, 2016
May 13, 2016
261
262
263
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
264
265
266
* `actual` {any}
* `expected` {any}
* `message` {any}
Apr 12, 2017
Apr 12, 2017
267
* `operator` {string} (default: '!=')
Nov 13, 2015
Nov 13, 2015
268
Dec 28, 2015
Dec 28, 2015
269
270
271
272
Throws an `AssertionError`. If `message` is falsy, the error message is set as
the values of `actual` and `expected` separated by the provided `operator`.
Otherwise, the error message is the value of `message`.
Jan 21, 2016
Jan 21, 2016
273
274
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
275
Jan 21, 2016
Jan 21, 2016
276
assert.fail(1, 2, undefined, '>');
Nov 16, 2016
Nov 16, 2016
277
// AssertionError: 1 > 2
Dec 28, 2015
Dec 28, 2015
278
Jan 21, 2016
Jan 21, 2016
279
assert.fail(1, 2, 'whoops', '>');
Nov 16, 2016
Nov 16, 2016
280
// AssertionError: whoops
Apr 12, 2017
Apr 12, 2017
281
282
283
284
285
286
assert.fail('boom');
// AssertionError: boom
assert.fail('a', 'b');
// AssertionError: 'a' != 'b'
Jan 21, 2016
Jan 21, 2016
287
```
Nov 13, 2015
Nov 13, 2015
288
289
## assert.ifError(value)
May 13, 2016
May 13, 2016
290
291
292
<!-- YAML
added: v0.1.97
-->
Feb 27, 2017
Feb 27, 2017
293
* `value` {any}
Nov 13, 2015
Nov 13, 2015
294
295
296
297
Throws `value` if `value` is truthy. This is useful when testing the `error`
argument in callbacks.
Jan 21, 2016
Jan 21, 2016
298
299
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
300
Nov 16, 2016
Nov 16, 2016
301
302
303
304
305
306
307
308
assert.ifError(0);
// OK
assert.ifError(1);
// Throws 1
assert.ifError('error');
// Throws 'error'
assert.ifError(new Error());
// Throws Error
Jan 21, 2016
Jan 21, 2016
309
```
Dec 28, 2015
Dec 28, 2015
310
Sep 25, 2014
Sep 25, 2014
311
## assert.notDeepEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
312
313
314
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
315
316
317
* `actual` {any}
* `expected` {any}
* `message` {any}
Oct 28, 2010
Oct 28, 2010
318
Feb 2, 2016
Feb 2, 2016
319
Tests for any deep inequality. Opposite of [`assert.deepEqual()`][].
Oct 28, 2010
Oct 28, 2010
320
Jan 21, 2016
Jan 21, 2016
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
```js
const assert = require('assert');
const obj1 = {
a : {
b : 1
}
};
const obj2 = {
a : {
b : 2
}
};
const obj3 = {
a : {
b : 1
}
Aug 1, 2016
Aug 1, 2016
338
};
Jan 21, 2016
Jan 21, 2016
339
340
const obj4 = Object.create(obj1);
Jan 31, 2016
Jan 31, 2016
341
assert.notDeepEqual(obj1, obj1);
Nov 16, 2016
Nov 16, 2016
342
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
Jan 21, 2016
Jan 21, 2016
343
Jan 31, 2016
Jan 31, 2016
344
assert.notDeepEqual(obj1, obj2);
Nov 16, 2016
Nov 16, 2016
345
// OK, obj1 and obj2 are not deeply equal
Jan 21, 2016
Jan 21, 2016
346
Jan 31, 2016
Jan 31, 2016
347
assert.notDeepEqual(obj1, obj3);
Nov 16, 2016
Nov 16, 2016
348
// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
Jan 21, 2016
Jan 21, 2016
349
Jan 31, 2016
Jan 31, 2016
350
assert.notDeepEqual(obj1, obj4);
Nov 16, 2016
Nov 16, 2016
351
// OK, obj1 and obj2 are not deeply equal
Jan 21, 2016
Jan 21, 2016
352
```
Dec 28, 2015
Dec 28, 2015
353
354
355
356
357
If the values are deeply equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned.
Nov 13, 2015
Nov 13, 2015
358
## assert.notDeepStrictEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
359
360
361
<!-- YAML
added: v1.2.0
-->
Feb 27, 2017
Feb 27, 2017
362
363
364
* `actual` {any}
* `expected` {any}
* `message` {any}
Oct 28, 2010
Oct 28, 2010
365
Feb 2, 2016
Feb 2, 2016
366
Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
Dec 28, 2015
Dec 28, 2015
367
Jan 21, 2016
Jan 21, 2016
368
369
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
370
Jan 21, 2016
Jan 21, 2016
371
assert.notDeepEqual({a:1}, {a:'1'});
Nov 16, 2016
Nov 16, 2016
372
// AssertionError: { a: 1 } notDeepEqual { a: '1' }
Dec 28, 2015
Dec 28, 2015
373
Jan 21, 2016
Jan 21, 2016
374
assert.notDeepStrictEqual({a:1}, {a:'1'});
Nov 16, 2016
Nov 16, 2016
375
// OK
Jan 21, 2016
Jan 21, 2016
376
```
Dec 28, 2015
Dec 28, 2015
377
378
379
380
If the values are deeply and strictly equal, an `AssertionError` is thrown
with a `message` property set equal to the value of the `message` parameter. If
the `message` parameter is undefined, a default error message is assigned.
Nov 13, 2015
Nov 13, 2015
381
382
## assert.notEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
383
384
385
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
386
387
388
* `actual` {any}
* `expected` {any}
* `message` {any}
Nov 13, 2015
Nov 13, 2015
389
Feb 27, 2017
Feb 27, 2017
390
Tests shallow, coercive inequality with the [Abstract Equality Comparison][]
Nov 13, 2015
Nov 13, 2015
391
( `!=` ).
Oct 28, 2010
Oct 28, 2010
392
Jan 21, 2016
Jan 21, 2016
393
394
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
395
Jan 21, 2016
Jan 21, 2016
396
assert.notEqual(1, 2);
Nov 16, 2016
Nov 16, 2016
397
// OK
Dec 28, 2015
Dec 28, 2015
398
Jan 21, 2016
Jan 21, 2016
399
assert.notEqual(1, 1);
Nov 16, 2016
Nov 16, 2016
400
// AssertionError: 1 != 1
Dec 28, 2015
Dec 28, 2015
401
Jan 21, 2016
Jan 21, 2016
402
assert.notEqual(1, '1');
Nov 16, 2016
Nov 16, 2016
403
// AssertionError: 1 != '1'
Jan 21, 2016
Jan 21, 2016
404
```
Dec 28, 2015
Dec 28, 2015
405
406
407
408
409
If the values are equal, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is undefined, a default error message is assigned.
Sep 25, 2014
Sep 25, 2014
410
## assert.notStrictEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
411
412
413
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
414
415
416
* `actual` {any}
* `expected` {any}
* `message` {any}
Oct 28, 2010
Oct 28, 2010
417
Feb 27, 2017
Feb 27, 2017
418
Tests strict inequality as determined by the [Strict Equality Comparison][]
Sep 19, 2015
Sep 19, 2015
419
( `!==` ).
Feb 9, 2015
Feb 9, 2015
420
Jan 21, 2016
Jan 21, 2016
421
422
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
423
Jan 21, 2016
Jan 21, 2016
424
assert.notStrictEqual(1, 2);
Nov 16, 2016
Nov 16, 2016
425
// OK
Dec 28, 2015
Dec 28, 2015
426
Jan 21, 2016
Jan 21, 2016
427
assert.notStrictEqual(1, 1);
Nov 22, 2016
Nov 22, 2016
428
// AssertionError: 1 !== 1
Dec 28, 2015
Dec 28, 2015
429
Jan 21, 2016
Jan 21, 2016
430
assert.notStrictEqual(1, '1');
Nov 16, 2016
Nov 16, 2016
431
// OK
Jan 21, 2016
Jan 21, 2016
432
```
Dec 28, 2015
Dec 28, 2015
433
434
435
436
437
If the values are strictly equal, an `AssertionError` is thrown with a
`message` property set equal to the value of the `message` parameter. If the
`message` parameter is undefined, a default error message is assigned.
Jan 30, 2016
Jan 30, 2016
438
## assert.ok(value[, message])
May 13, 2016
May 13, 2016
439
440
441
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
442
443
* `value` {any}
* `message` {any}
Jan 30, 2016
Jan 30, 2016
444
445
446
447
448
449
450
451
452
453
454
Tests if `value` is truthy. It is equivalent to
`assert.equal(!!value, true, message)`.
If `value` is not truthy, an `AssertionError` is thrown with a `message`
property set equal to the value of the `message` parameter. If the `message`
parameter is `undefined`, a default error message is assigned.
```js
const assert = require('assert');
Nov 16, 2016
Nov 16, 2016
455
456
457
458
assert.ok(true);
// OK
assert.ok(1);
// OK
Jan 30, 2016
Jan 30, 2016
459
assert.ok(false);
Nov 16, 2016
Nov 16, 2016
460
// throws "AssertionError: false == true"
Jan 30, 2016
Jan 30, 2016
461
assert.ok(0);
Nov 16, 2016
Nov 16, 2016
462
// throws "AssertionError: 0 == true"
Jan 30, 2016
Jan 30, 2016
463
assert.ok(false, 'it\'s false');
Nov 16, 2016
Nov 16, 2016
464
// throws "AssertionError: it's false"
Jan 30, 2016
Jan 30, 2016
465
466
```
Nov 13, 2015
Nov 13, 2015
467
## assert.strictEqual(actual, expected[, message])
May 13, 2016
May 13, 2016
468
469
470
<!-- YAML
added: v0.1.21
-->
Feb 27, 2017
Feb 27, 2017
471
472
473
* `actual` {any}
* `expected` {any}
* `message` {any}
Feb 9, 2015
Feb 9, 2015
474
Feb 27, 2017
Feb 27, 2017
475
476
Tests strict equality as determined by the [Strict Equality Comparison][]
( `===` ).
Oct 28, 2010
Oct 28, 2010
477
Jan 21, 2016
Jan 21, 2016
478
479
```js
const assert = require('assert');
Dec 28, 2015
Dec 28, 2015
480
Jan 21, 2016
Jan 21, 2016
481
assert.strictEqual(1, 2);
Nov 16, 2016
Nov 16, 2016
482
// AssertionError: 1 === 2
Dec 28, 2015
Dec 28, 2015
483
Jan 21, 2016
Jan 21, 2016
484
assert.strictEqual(1, 1);
Nov 16, 2016
Nov 16, 2016
485
// OK
Dec 28, 2015
Dec 28, 2015
486
Jan 21, 2016
Jan 21, 2016
487
assert.strictEqual(1, '1');
Nov 16, 2016
Nov 16, 2016
488
// AssertionError: 1 === '1'
Jan 21, 2016
Jan 21, 2016
489
```
Dec 28, 2015
Dec 28, 2015
490
491
492
493
494
If the values are not strictly equal, an `AssertionError` is thrown with a
`message` property set equal to the value of the `message` parameter. If the
`message` parameter is undefined, a default error message is assigned.
Sep 29, 2014
Sep 29, 2014
495
## assert.throws(block[, error][, message])
May 13, 2016
May 13, 2016
496
497
<!-- YAML
added: v0.1.21
Feb 24, 2017
Feb 24, 2017
498
499
500
501
changes:
- version: v4.2.0
pr-url: https://github.com/nodejs/node/pull/3276
description: The `error` parameter can now be an arrow function.
May 13, 2016
May 13, 2016
502
-->
Feb 27, 2017
Feb 27, 2017
503
504
505
* `block` {Function}
* `error` {RegExp|Function}
* `message` {any}
Oct 28, 2010
Oct 28, 2010
506
Apr 4, 2016
Apr 4, 2016
507
508
509
510
511
512
513
Expects the function `block` to throw an error.
If specified, `error` can be a constructor, [`RegExp`][], or validation
function.
If specified, `message` will be the message provided by the `AssertionError` if
the block fails to throw.
Dec 2, 2010
Dec 2, 2010
514
515
516
Validate instanceof using constructor:
Jan 21, 2016
Jan 21, 2016
517
518
```js
assert.throws(
Jan 27, 2016
Jan 27, 2016
519
() => {
Jan 21, 2016
Jan 21, 2016
520
521
522
523
524
throw new Error('Wrong value');
},
Error
);
```
Dec 2, 2010
Dec 2, 2010
525
Dec 3, 2015
Dec 3, 2015
526
Validate error message using [`RegExp`][]:
Dec 2, 2010
Dec 2, 2010
527
Jan 21, 2016
Jan 21, 2016
528
529
```js
assert.throws(
Jan 27, 2016
Jan 27, 2016
530
() => {
Jan 21, 2016
Jan 21, 2016
531
532
533
534
535
throw new Error('Wrong value');
},
/value/
);
```
Dec 2, 2010
Dec 2, 2010
536
537
538
Custom error validation:
Jan 21, 2016
Jan 21, 2016
539
540
```js
assert.throws(
Jan 27, 2016
Jan 27, 2016
541
() => {
Jan 21, 2016
Jan 21, 2016
542
543
544
545
546
547
548
549
550
551
throw new Error('Wrong value');
},
function(err) {
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
}
},
'unexpected error'
);
```
Nov 16, 2015
Nov 16, 2015
552
Apr 4, 2016
Apr 4, 2016
553
554
555
556
557
558
559
560
561
562
563
564
Note that `error` can not be a string. If a string is provided as the second
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:
```js
// THIS IS A MISTAKE! DO NOT DO THIS!
assert.throws(myFunction, 'missing foo', 'did not throw with expected message');
// Do this instead.
assert.throws(myFunction, /missing foo/, 'did not throw with expected message');
```
Feb 27, 2017
Feb 27, 2017
565
566
567
568
569
570
571
572
573
574
575
576
577
578
## Caveats
For the following cases, consider using ES2015 [`Object.is()`][],
which uses the [SameValueZero][] comparison.
```js
const a = 0;
const b = -a;
assert.notStrictEqual(a, b);
// AssertionError: 0 !== -0
// Strict Equality Comparison doesn't distinguish between -0 and +0...
assert(!Object.is(a, b));
// but Object.is() does!
Apr 19, 2017
Apr 19, 2017
579
580
const str1 = 'foo';
const str2 = 'foo';
Feb 27, 2017
Feb 27, 2017
581
582
583
584
585
586
587
588
589
590
assert.strictEqual(str1 / 1, str2 / 1);
// AssertionError: NaN === NaN
// Strict Equality Comparison can't be used to check NaN...
assert(Object.is(str1 / 1, str2 / 1));
// but Object.is() can!
```
For more information, see
[MDN's guide on equality comparisons and sameness][mdn-equality-guide].
Feb 2, 2016
Feb 2, 2016
591
592
[`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message
[`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message
Jan 30, 2016
Jan 30, 2016
593
[`assert.ok()`]: #assert_assert_ok_value_message
Dec 3, 2015
Dec 3, 2015
594
595
[`assert.throws()`]: #assert_assert_throws_block_error_message
[`Error`]: errors.html#errors_class_error
Apr 3, 2017
Apr 3, 2017
596
[caveats]: #assert_caveats
Dec 3, 2015
Dec 3, 2015
597
[`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Dec 17, 2015
Dec 17, 2015
598
[`TypeError`]: errors.html#errors_class_typeerror
Feb 27, 2017
Feb 27, 2017
599
600
[Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison
[Strict Equality Comparison]: https://tc39.github.io/ecma262/#sec-strict-equality-comparison
Apr 3, 2017
Apr 3, 2017
601
602
[`Map`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
[`Set`]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Set
Feb 27, 2017
Feb 27, 2017
603
604
605
606
[`Object.is()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
[SameValueZero]: https://tc39.github.io/ecma262/#sec-samevaluezero
[prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
[mdn-equality-guide]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
Mar 12, 2017
Mar 12, 2017
607
608
[enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring