Skip to content

Latest commit

Β 

History

History
643 lines (523 loc) Β· 17.1 KB

assert.md

File metadata and controls

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