NodeJS Assertion: How to Use the NodeJS Assert Module

NodeJS Assert Module provides a way of testing the expression of a particular condition, if it is true then nothing happens, and the rest of the program is executed without breaking the flow, but if it is false then the asserted error is thrown in the shell and terminate the rest of the code. 

We can use NodeJS Assertion to write test cases. The Assert Module provides various methods used to perform assertions.

NodeJS Assert Module

Node.js Assert Module is a built-in Node.js module, so it is not required to install it, we just have to import it using the below syntax.

Syntax:

const assert = require('assert');

Methods within the NodeJS Assert Module

1. assert()

This method takes an expression and a message as parameters, if the expression value is true then nothing happens, if it is false then the assertion fails and an error throws containing the message in the terminal.

Syntax:

assert(expression, message)
  • expression: Specifies the conditions
  • message: Specifies the error message

Example:

const assert = require('assert');
assert(2 == 4, "2 is not equal to 4"); // 2 == 4 is false, so this will throw an error

Output:

Assert Example

2. deepEqual()

This method takes two objects as an argument to check whether both object and their child objects are equal using the == operator, if it is not equal then it returns false and the assertion fails. This method takes a message as an optional argument which is thrown if the assertion fails.

Syntax:

assert.deepEqual(obj, obj, message)
  • obj: Specifies the object for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
const hello = { text: { value: "Hello" } }
const world = { text: { value: "World!" } }
assert.deepEqual(hello, world, "Hello is not equal to World!"); // "Hello" == "World!" is false, so this will throw an error

Output:

DeepEqual Example

3. notDeepEqual()

This method takes two objects as an argument to check whether both object and their child objects are not equal using the != operator, if it is equal then it returns false and the assertion fails. This method takes a message as an optional argument which is thrown if the assertion fails.

Syntax:

assert.notDeepEqual(obj, obj, message)
  • obj: Specifies the object for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
const num1 = { number: { value: 1 } }
const num2 = { number: { value: "1" } }
assert.notDeepEqual(num1, num2, "Values are equal!"); // This will fail because the values are equal

Output:

NotDeepEqual Example

4. deepStrictEqual

This method is similar to the deepEqual() method, but instead of using the == operator, this method uses the === operator.

Syntax:

assert.deepStrictEqual(obj, obj, message)
  • obj: Specifies the object for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
const num1 = { number: { value: "1" } }
const num2 = { number: { value: 1 } }
assert.deepStrictEqual(num1, num2, "Value are not strictly equal!"); // "1" === 1 is false, so this will throw an error

Output:

DeepStrictEqual Example

5. notDeepStrictEqual

This method is similar to the notDeepEqual() method, but instead of using the != operator, this method uses the !== operator.

Syntax:

assert.notDeepStrictEqual(obj, obj, message)
  • obj: Specifies the object for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
const num1 = { number: { value: 1 } } 
const num2 = { number: { value: 1 } }
assert.notDeepStrictEqual(num1, num2, "Values are strictly equal!"); // This will fail because the values are strictly equal

Output:

NotDeepStrictEqual Example

6. equal()

This method takes two values as an argument to check whether both values are equal or not using the == operator, if it is equal then it returns true, if not then returns false. This method takes a message as an optional argument which throws if the assertion fails.

Syntax:

assert.equal(value, value, message)
  • value: Specifies the value for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
assert.equal(1, 2, "Values are not equal!"); // 1 == 2 is false, so it will throw an error

Output:

Equal Example

7. notEqual()

This method is the opposite of the method equal(), which uses the != operator to perform the assertion, which fails when both values are equal.

Syntax:

assert.notEqual(value, value, message)
  • value: Specifies the value for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
assert.notEqual(1, 1, "Values are equal!"); // 1 != 1 is false, so it will throw an error

Output:

NotEqual Example

8. strictEqual()

This method is similar to the equal() method, but it uses the === operator to perform assertion whereas the equal() method uses the == operator.

Syntax:

assert.strictEqual(value, value, message)
  • value: Specifies the value for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
assert.strictEqual(1, "1", "Values are not strictly equal!");  // 1 === "1" => false

Output:

StrictEqual Example

9. notStrictEqual()

This method is the opposite of the method strictEqual(), it uses the !== operator to perform the assertion, which fails when both values are strictly equal.

Syntax:

assert.notStrictEqual(value, value, message)
  • value: Specifies the value for the comparison
  • message: Specifies the error message

Example:

const assert = require('assert');
assert.notStrictEqual(1, "1", "Values are strictly equal!");  // 1 != "1" => true
assert.notStrictEqual(1, 1, "Values are strictly equal!");  // 1 != 1 => false

Output:

NotStrictEqual Example

10. ok()

This method is completely identical to the assert() method.

Syntax:

assert.ok(expression, message)
  • expression: Specifies the conditions
  • message: Specifies the error message

Example:

const assert = require('assert');
assert.ok(1 == 1, "1 is not equal to 1");  // 1 == 1 => true
assert.ok(1 == 2, "1 is not equal to 2");  // 1 == 2 => false

Output:

Ok Example

Summary

Node.js Assert Module is a built-in module, used to perform various types of assertion. There are various methods with slightly different functionalities, it is important to understand the difference between them, to write proper tests in Node.js. However, the Assert Module is not recommended to use for testing, but still, it can be used in unit testing when it is required to test a small expression for some conditions. We hope this tutorial helped you to understand the Node.js Assertion.

Reference

https://nodejs.org/api/assert.html

Aditya Gupta
Aditya Gupta
Articles: 161