Assert module in node.js is an inbuilt module which can be used to write tests. It provides a way to perform unit tests on node.js expressions. If the test case
is failed or 0 or false then , an error is raised. If the test case is passed or 1 or true then it provides no feedback.
Although this module is intended for only internal use in node.js but we can still access it using the following :
var assert = require('assert');
assert
is not a testing framework and we must not treat it like one.
Some common examples are given below which explains how we can use assert
module of node.js in our code.
a
is greater that variable b
or not.
//Name of the file : assert-1.js
var assert = require('assert');
var a = 10;
var b = 20;
assert(a > b);
>node assert-1.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: false == true
//Name of the file : assert-snippet-true.js
var assert = require('assert');
function demo (x,y,z) {
var value = x + y + z ;
return value;
}
var output = demo(4,1,10);
console.log("Output : " + output);
var expected_output = 15;
console.log("Expected Output : " + expected_output);
assert( output === expected_output , 'Test case is true so this will not be printed');
>node assert-snippet-true.js
Output : 15
Expected Output : 15
true
, it does not return any output.
//Name of the file : assert-snippet-false.js
var assert = require('assert');
function demo (x,y,z) {
var value = x + y + z ;
return value;
}
var output = demo(3,2,10);
console.log("Output : " + output);
var expected_output = 12;
console.log("Expected Output : " + expected_output);
assert( output === expected_output , 'This is not what we expected');
>node assert-snippet-false.js
Output : 15
Expected Output : 12
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: This is not what we expected
assert
module
Each and every method of assert
module of node.js is explained in detail below with code examples.
assert.ok()
method.
assert(value[, message])
//Name of the file : assert.js
var assert = require('assert');
var a = 10;
var b = 20;
assert(a > b , "A should be greater than B");
>node assert.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: A should be greater than B
==
operator.
assert.deepEqual(actual, expected[, message])
//Name of the file : assert.deepEqual.js
var assert = require('assert');
var a = 10;
var b = '10';
var c = 10.25;
//Case 1
assert.deepEqual(a,b, "Nothing printed because they are using == for comparison");
//Case 2
assert.deepEqual(a,c, "Error because values doesn't match");
>node assert.deepEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because values doesn't match
===
operator.
assert.deepStrictEqual(actual, expected[, message])
//Name of the file : assert.deepStrictEqual.js
var assert = require('assert');
var a = 10;
var b = '10';
assert.deepStrictEqual(a,b, "Error because they are using === for comparison");
>node assert.deepStrictEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because they are using === for comparison
==
operator.
assert.equal(actual, expected[, message])
//Name of the file : assert.equal.js
var assert = require('assert');
var a = 50;
var b = '50';
var c = 50.25;
//Case 1
assert.equal(a,b, "Nothing printed because they are using == for comparison");
//Case 2
assert.equal(a,c, "Error because values doesn't match");
>node assert.equal.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because values doesn't match
//Case 1
assert.fail(message)
//Case 2
assert.fail(actual, expected[, message[, operator[, stackStartFunction]]])
//Name of the file : assert.fail.js
var assert = require('assert');
assert.fail(1, 2, 'This is an error', '>');
>node assert.fail.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: This is an error
assert.ifError(value)
//Name of the file : assert.ifError.js
var assert = require('assert');
//Case 1 : No error
assert.ifError(0);
//Case 2 : throws 1
assert.ifError(1);
//Case 3 : throws error
assert.ifError('error');
>node assert.ifError.js
assert.js:644
assert.ifError = function ifError(err) { if (err) throw err; };
^
1
assert.deepEqual()
method.
assert.notDeepEqual(actual, expected[, message])
//Name of the file : assert.notDeepEqual.js
var assert = require('assert');
var a = 10;
var b = 12;
var c = '10';
//Case 1
assert.notDeepEqual(a,b, "Nothing printed because they are using !== for comparison");
//Case 2
assert.notDeepEqual(a,c, "Error because values match here");
>node assert.notDeepEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because values match here
assert.deepStrictEqual()
method.
assert.notDeepStrictEqual(actual, expected[, message])
//Name of the file : assert.notDeepStrictEqual.js
var assert = require('assert');
var a = 10;
var b = '10';
var c = 10;
//Case 1
assert.notDeepStrictEqual(a,b, "No Error because they are checking for not deep strict equal");
//Case 2
assert.notDeepStrictEqual(a,c, "Error because values are equal");
>node assert.notDeepStrictEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because values are equal
!=
( not equal operator ).
assert.notEqual(actual, expected[, message])
//Name of the file : assert.notEqual.js
var assert = require('assert');
var a = 10;
var b = 10.25;
var c = '10';
//Case 1
assert.notEqual(a,b, "Nothing printed because they are using != for comparison");
//Case 2
assert.notEqual(a,c, "Error because values match");
>node assert.notEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because values match
!==
( strict not equal operator ).
assert.notStrictEqual(actual, expected[, message])
//Name of the file : assert.notStrictEqual.js
var assert = require('assert');
var a = 10;
var b = 10.25;
var c = '10';
var d = 10;
//Case 1
assert.notStrictEqual(a,b, "Nothing printed because they are using !== for comparison");
//Case 2
assert.notStrictEqual(a,c, "Nothing printed because still its not a match");
//case 3
assert.notStrictEqual(a,d, "Error because its a match");
>node assert.notStrictEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error because its a match
assert.ok(value[, message])
//Name of the file : assert.ok.js
var assert = require('assert');
//Case 1
assert.ok(true, "No error ");
//Case 2
assert.ok(1,"No error");
//Case 3
assert.ok(false,"It is an error");
//Case 4
assert.ok(0 , " Again error");
//case 5
var a = 10;
var b = 20;
assert(a > b , "A should be greater than B");
>node assert.ok.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: It is an error
case 3
and case 4
to see the output of case 5
and in the same way
comment case 3
and case 5
to see the output of case 4
.
===
( strict equal operator ).
assert.strictEqual(actual, expected[, message])
//Name of the file : assert.strictEqual.js
var assert = require('assert');
var a = 10;
var b = '10';
var c = 10;
//Case 1
assert.strictEqual(a,c, "Nothing printed");
//Case 2
assert.strictEqual(a,b, "Error acc to strict equality comparison");
>node assert.strictEqual.js
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: Error acc to strict equality comparison
assert.throws(block[, error][, message])
//Name of the file : assert.throw.js
var assert = require('assert');
assert.throws(
() => {
throw new Error('Wrong value');
},
Error
);
assert.throws
method.
assert.doesNotThrow(block[, error][, message])
In this chapter of 30 days of node , we learned about what is assert module and how we can use it in our code.
We also learned about all the methods of assert module with code examples. Methods include : assert()
, assert.deepEqual()
,
assert.deepStrictEqual()
, assert.doesNotThrow()
, assert.equal()
, assert.fail()
,
assert.ifError()
, assert.notDeepEqual()
, assert.notDeepStrictEqual()
, assert.notEqual()
,
assert.notStrictEqual()
, assert.ok()
, assert.strictEqual()
, assert.throws()
.