5 The `assert` module provides a simple set of assertion tests that can be used to
6 test invariants. The module is intended for internal use by Node.js, but can be
7 used in application code via `require('assert')`. However, `assert` is not a
8 testing framework, and is not intended to be used as a general purpose assertion
11 The API for the `assert` module is [Locked][]. This means that there will be no
12 additions or changes to any of the methods implemented and exposed by
15 ## assert(value[, message]), assert.ok(value[, message])
17 Tests if `value` is truthy. It is equivalent to
18 `assert.equal(!!value, true, message)`.
20 If `value` is not truthy, an `AssertionError` is thrown with a `message`
21 property set equal to the value of the `message` parameter. If the `message`
22 parameter is `undefined`, a default error message is assigned.
25 const assert = require('assert');
30 // throws "AssertionError: false == true"
32 // throws "AssertionError: 0 == true"
33 assert(false, 'it\'s false');
34 // throws "AssertionError: it's false"
36 assert.ok(true); // OK
39 // throws "AssertionError: false == true"
41 // throws "AssertionError: 0 == true"
42 assert.ok(false, 'it\'s false');
43 // throws "AssertionError: it's false"
46 ## assert.deepEqual(actual, expected[, message])
48 Tests for deep equality between the `actual` and `expected` parameters.
49 Primitive values are compared with the equal comparison operator ( `==` ).
51 Only enumerable "own" properties are considered. The `deepEqual()`
52 implementation does not test object prototypes, attached symbols, or
53 non-enumerable properties. This can lead to some potentially surprising
54 results. For example, the following example does not throw an `AssertionError`
55 because the properties on the [`Error`][] object are non-enumerable:
58 // WARNING: This does not throw an AssertionError!
59 assert.deepEqual(Error('a'), Error('b'));
62 "Deep" equality means that the enumerable "own" properties of child objects
66 const assert = require('assert');
83 const obj4 = Object.create(obj1);
85 assert.deepEqual(obj1, obj1);
86 // OK, object is equal to itself
88 assert.deepEqual(obj1, obj2);
89 // AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
90 // values of b are different
92 assert.deepEqual(obj1, obj3);
93 // OK, objects are equal
95 assert.deepEqual(obj1, obj4);
96 // AssertionError: { a: { b: 1 } } deepEqual {}
97 // Prototypes are ignored
100 If the values are not equal, an `AssertionError` is thrown with a `message`
101 property set equal to the value of the `message` parameter. If the `message`
102 parameter is undefined, a default error message is assigned.
104 ## assert.deepStrictEqual(actual, expected[, message])
106 Generally identical to `assert.deepEqual` with the exception that primitive
107 values are compared using the strict equality operator ( `===` ).
110 const assert = require('assert');
112 assert.deepEqual({a:1}, {a:'1'});
113 // OK, because 1 == '1'
115 assert.deepStrictEqual({a:1}, {a:'1'});
116 // AssertionError: { a: 1 } deepStrictEqual { a: '1' }
117 // because 1 !== '1' using strict equality
120 If the values are not equal, an `AssertionError` is thrown with a `message`
121 property set equal to the value of the `message` parameter. If the `message`
122 parameter is undefined, a default error message is assigned.
124 ## assert.doesNotThrow(block[, error][, message])
126 Asserts that the function `block` does not throw an error. See
127 [`assert.throws()`][] for more details.
129 When `assert.doesNotThrow()` is called, it will immediately call the `block`
132 If an error is thrown and it is the same type as that specified by the `error`
133 parameter, then an `AssertionError` is thrown. If the error is of a different
134 type, or if the `error` parameter is undefined, the error is propagated back
137 The following, for instance, will throw the [`TypeError`][] because there is no
138 matching error type in the assertion:
143 throw new TypeError('Wrong value');
149 However, the following will result in an `AssertionError` with the message
150 'Got unwanted exception (TypeError)..':
155 throw new TypeError('Wrong value');
161 If an `AssertionError` is thrown and a value is provided for the `message`
162 parameter, the value of `message` will be appended to the `AssertionError`
168 throw new TypeError('Wrong value');
173 // Throws: AssertionError: Got unwanted exception (TypeError). Whoops
176 ## assert.equal(actual, expected[, message])
178 Tests shallow, coercive equality between the `actual` and `expected` parameters
179 using the equal comparison operator ( `==` ).
182 const assert = require('assert');
186 assert.equal(1, '1');
190 // AssertionError: 1 == 2
191 assert.equal({a: {b: 1}}, {a: {b: 1}});
192 //AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
195 If the values are not equal, an `AssertionError` is thrown with a `message`
196 property set equal to the value of the `message` parameter. If the `message`
197 parameter is undefined, a default error message is assigned.
199 ## assert.fail(actual, expected, message, operator)
201 Throws an `AssertionError`. If `message` is falsy, the error message is set as
202 the values of `actual` and `expected` separated by the provided `operator`.
203 Otherwise, the error message is the value of `message`.
206 const assert = require('assert');
208 assert.fail(1, 2, undefined, '>');
209 // AssertionError: 1 > 2
211 assert.fail(1, 2, 'whoops', '>');
212 // AssertionError: whoops
215 ## assert.ifError(value)
217 Throws `value` if `value` is truthy. This is useful when testing the `error`
218 argument in callbacks.
221 const assert = require('assert');
223 assert.ifError(0); // OK
224 assert.ifError(1); // Throws 1
225 assert.ifError('error') // Throws 'error'
226 assert.ifError(new Error()); // Throws Error
229 ## assert.notDeepEqual(actual, expected[, message])
231 Tests for any deep inequality. Opposite of [`assert.deepEqual`][].
234 const assert = require('assert');
251 const obj4 = Object.create(obj1);
253 assert.deepEqual(obj1, obj1);
254 AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
256 assert.deepEqual(obj1, obj2);
257 // OK, obj1 and obj2 are not deeply equal
259 assert.deepEqual(obj1, obj3);
260 // AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }
262 assert.deepEqual(obj1, obj4);
263 // OK, obj1 and obj2 are not deeply equal
266 If the values are deeply equal, an `AssertionError` is thrown with a `message`
267 property set equal to the value of the `message` parameter. If the `message`
268 parameter is undefined, a default error message is assigned.
270 ## assert.notDeepStrictEqual(actual, expected[, message])
272 Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual`][].
275 const assert = require('assert');
277 assert.notDeepEqual({a:1}, {a:'1'});
278 // AssertionError: { a: 1 } notDeepEqual { a: '1' }
280 assert.notDeepStrictEqual({a:1}, {a:'1'});
284 If the values are deeply and strictly equal, an `AssertionError` is thrown
285 with a `message` property set equal to the value of the `message` parameter. If
286 the `message` parameter is undefined, a default error message is assigned.
288 ## assert.notEqual(actual, expected[, message])
290 Tests shallow, coercive inequality with the not equal comparison operator
294 const assert = require('assert');
296 assert.notEqual(1, 2);
299 assert.notEqual(1, 1);
300 // AssertionError: 1 != 1
302 assert.notEqual(1, '1');
303 // AssertionError: 1 != '1'
306 If the values are equal, an `AssertionError` is thrown with a `message`
307 property set equal to the value of the `message` parameter. If the `message`
308 parameter is undefined, a default error message is assigned.
310 ## assert.notStrictEqual(actual, expected[, message])
312 Tests strict inequality as determined by the strict not equal operator
316 const assert = require('assert');
318 assert.notStrictEqual(1, 2);
321 assert.notStrictEqual(1, 1);
322 // AssertionError: 1 != 1
324 assert.notStrictEqual(1, '1');
328 If the values are strictly equal, an `AssertionError` is thrown with a
329 `message` property set equal to the value of the `message` parameter. If the
330 `message` parameter is undefined, a default error message is assigned.
332 ## assert.strictEqual(actual, expected[, message])
334 Tests strict equality as determined by the strict equality operator ( `===` ).
337 const assert = require('assert');
339 assert.strictEqual(1, 2);
340 // AssertionError: 1 === 2
342 assert.strictEqual(1, 1);
345 assert.strictEqual(1, '1');
346 // AssertionError: 1 === '1'
349 If the values are not strictly equal, an `AssertionError` is thrown with a
350 `message` property set equal to the value of the `message` parameter. If the
351 `message` parameter is undefined, a default error message is assigned.
353 ## assert.throws(block[, error][, message])
355 Expects the function `block` to throw an error. If specified, `error` can be a
356 constructor, [`RegExp`][], or validation function.
358 Validate instanceof using constructor:
363 throw new Error('Wrong value');
369 Validate error message using [`RegExp`][]:
374 throw new Error('Wrong value');
380 Custom error validation:
385 throw new Error('Wrong value');
388 if ( (err instanceof Error) && /value/.test(err) ) {
396 [Locked]: documentation.html#documentation_stability_index
397 [`assert.deepEqual`]: #assert_assert_deepequal_actual_expected_message
398 [`assert.deepStrictEqual`]: #assert_assert_deepstrictequal_actual_expected_message
399 [`assert.throws()`]: #assert_assert_throws_block_error_message
400 [`Error`]: errors.html#errors_class_error
401 [`RegExp`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
402 [`TypeError`]: errors.html#errors_class_typeerror