• Node.js Video Tutorials

Node.js - Assert Module



Assert module in Node.js provides a collection of assertion functions that are used in verifying invariants. If the condition is evaluated to false, or 0, an assertion error will be returned, and the program is terminated. If it is evaluated to true, nothing is returned to the output.

Installation

This installation is optional because assert is an inbuilt Node.js module.

  • Open the command prompt and navigate to your working folder (to the folder where you saved your file.js).

  • Now type the command which is in the snippet below. With this, you can install the assert module package.

npm install assert	
  • Now, you are free to use all the functions available in the assert module.

  • After installation is done, you can check the version of the assert module by using the command which is in the below snippet.

npm version assert

List of Functions

Following is a list of functions available in the Assert Class −

Sr.No Function & Description
1

assert()

Used to check, if the value is truthy

2

deepEqual()

Used to test for equality between the input parameters.

3

deepStrictEqual()

Used to "deep" test for equality between the input parameters.

4

doesNotMatch()

It will expect the input string not to match the regular expression.

5

doesNotReject()

Used to get the endianness of the CPU.

6

doesNotThrow()

Used to assert that a function or expression does not throw an error when it is executed.

7

equal()

Used to test for equality between the input parameters.

8

fail()

It will throw an assertion error with the given error message.

9

ifError()

It will throw assertion error, if the value passed is undefined or null.

10

notDeepEqual()

Used to test for deep inequality.

11

notDeepStrictEqual()

Used to test for deep "strict" inequality.

12

notEqual()

Used to test inequality between the input parameters.

13

notStrictEqual()

Used to test "strict" inequality between the input parameters.

14

ok()

Used to test if the input value is truthy.

15

rejects()

Used to return the total amount of system memory as a number of bytes.

16

strictEqual()

Used to check if two values are equal.

17

throws()

It will except the input function to throw an error.

Class: CallTracker Functions

These functions are currently experimental and behavior might still change.

Sr.No Function & Description
1

new assert.CallTracker()

Used to create a new CallTracker object, that can be used to track if functions were called a specific number of times.

2

report()

Used to get the information about expected and actual number of calls of the functions that have not been called the expected number of times.

3

verify()

Used to verify how many times function was actually called compared to expected number of calls.

Advertisements