THE WORLD'S LARGEST WEB DEVELOPER SITE

Node.js Assert Module

❮ Built-in Modules


Example

If an expression evaluates to 0 or false, an error is thrown and the program is terminated:

var assert = require('assert');
assert(5 > 7);
Run example »

Defintion 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.


Syntax

The syntax for including the assert module in your application:

var assert = require('assert');

Assert Methods

Method Description
assert() Checks if a value is true. Same as assert.ok()
deepEqual() Checks if two values are equal
deepStrictEqual() Checks if two values are equal, using the strict equal operator (===)
doesNotThrow()  
equal() Checks if two values are equal, using the equal operator (==)
fail() Throws an Assertion Error
ifError() Throws a specified error if the specified error evaluates to true
notDeepEqual() Checks if two values are not equal
notDeepStrictEqual() Checks if two values are not equal, using the strict not equal operator (!==)
notEqual() Checks if two values are not equal, using the not equal operator (!=)
notStrict() Checks if two values are not equal, using the strict not equal operator (!==)
ok() Checks if a value is true
strictEqual() Checks if two values are equal, using the strict equal operator (===)
throws()  

❮ Built-in Modules