DEV Community

hjqueeen
hjqueeen

Posted on

Assert module

1. Definition and Usage

The assert module provides a way of testing expressions. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated.

This module was built to be used internally by Node.js.

2. Syntax

The syntax for including the assert module in your application:

var assert = require('assert');
Enter fullscreen mode Exit fullscreen mode

3. Usage

assert()

Checks if a value is true. Same as assert.ok()

var assert = require('assert');
assert(50 > 70); // AssertionError: false == true
Enter fullscreen mode Exit fullscreen mode

deepEqual()

Checks if two values are equal
If two objects, or their child objects, are not equal, an error is thrown and the program is terminated:

var assert = require('assert');
var x = { a : { n: 0 } };
var y = { a : { n: 0 } };
var z = { a : { n: 1 } };
assert.deepEqual(x, y); //OK
assert.deepEqual(x, z); /*AssertionError: { a: { n: 0 } } deepEqual {a: { n: 1 } }*/
Enter fullscreen mode Exit fullscreen mode

deepStrictEqual()

If two objects, or their child objects, are not equal (both in value and type), an error is thrown and the program is terminated:

var x = { a : { n: 0 } };
var y = { a : { n: 0 } };
var z = { a : { n: '0' } };
assert.deepStrictEqual(x, y); //OK
assert.deepStrictEqual(x, z); /*AssertionError: { a: { n: 0 } } deepStrictEqual {a: { n: '0' } }*/
Enter fullscreen mode Exit fullscreen mode

doesNotThrow()

equal()

Checks if two values are equal, using the equal operator (==)

assert.equal(50, 50); //OK
assert.equal(50, "50"); //OK
assert.equal(50, 70); /*AssertionError: 50 == 70 */
Enter fullscreen mode Exit fullscreen mode

fail()

Throws an Assertion Error

assert.ifError(value)

Throws a specified error if the specified error evaluates to true

notDeepEqual()

Checks if two values are not equal
If two objects, and their child objects, are equal, an error is thrown and the program is terminated:

var x = { a : { n: 0 } };
var y = { a : { n: 0 } };
var z = { a : { n: 1 } };
assert.notDeepEqual(x, z); //OK
assert.notDeepEqual(x, y); /*AssertionError: { a: { n: 0 } } notDeepEqual {a: { n: 0 } }*/
Enter fullscreen mode Exit fullscreen mode

notDeepStrictEqual()

Checks if two values are not equal, using the strict not equal operator (!==)

var x = { a : { n: 0 } };
var y = { a : { n: 0 } };
var z = { a : { n: '0' } };
assert.notDeepStrictEqual(x, z); //OK
assert.notDeepStrictEqual(x, y); /*AssertionError: { a: { n: 0 } } notDeepStrictEqual {a: { n: 0 } }*/
Enter fullscreen mode Exit fullscreen mode

notEqual()

Checks if two values are not equal, using the not equal operator (!=)

notStrictEqual()

Checks if two values are not equal, using the strict not equal operator (!==)

assert.ok(value[, message])

Checks if a value is true

assert.ok(50 > 70); // AssertionError: false == true
Enter fullscreen mode Exit fullscreen mode

strictEqual()

Checks if two values are equal, using the strict equal operator (===)

throws()

Learn more assert module

Top comments (0)