• Node.js Video Tutorials

Node.js - assert.verify() Function



The assert module provides a set of assertion functions for verifying invariants. The tracker.verify() function is an inbuilt function of the assert module of Node.js.

The Node.js tracker.verify() function iterates through the function(s) which are passed to the tracker.calls() function and an error will be thrown by tracker.verify() function if the function passed to tracker.calls() have not been called as per the expected number of times.

This function doesn't throw any error if the function is being called the expected number of times.

Syntax

Following is the syntax of Node.js tracker.verify() function

tracker.verify();

Parameters

This function doesn't accept any parameters.

Return type

If a function is not called as to the expected number of times, the tracker.verify() function will throw an error.

Example

In the following example,

  • Initially, we are creating a call tracker object.

  • Then we are wrapping a function func inside wrapper function funccall.

  • We are not passing any value to the exact parameter of the Node.js tracker.calls() function and it takes 1 as the default value. So, it was expected that the function func should be called at least one time.

const assert = require('assert');
const tracker = new assert.CallTracker();
function func() {}
const funccall = tracker.calls(func);
funccall();
funccall();
tracker.verify();

Output

/home/cg/root/63a002c52763b/main.js:3
const tracker = new assert.CallTracker();
   ^
   
TypeError: assert.CallTracker is not a constructor
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − Sometimes online compiler may not give us the expected result, so we are executing the above code locally.

As we can see in the output below, the function func is called two times so that the tracker.verify() function will throw an error.

track_verify

Example

In the following example, we are passing 4 to the exact parameter of the Node.js tracker.calls() function. So, it was expected that the function func should be called at least four times.

const assert = require('assert');
const tracker = new assert.CallTracker();
function func() {}
const funccall = tracker.calls(func, 4);
funccall();
funccall();
tracker.verify();

Output

/home/cg/root/63a002c52763b/main.js:3
const tracker = new assert.CallTracker();
   ^
   
TypeError: assert.CallTracker is not a constructor
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − Sometimes online compiler may not give us the expected result, so we are executing the above code locally.

As we can see in the output below, the function func is called only two times so that the tracker.verify() function will throw an error

track_verify

Example

In the following example below, the function func is called as per the expected number of times.

const assert = require('assert');
const tracker = new assert.CallTracker();
function func() {}
const funccall = tracker.calls(func, 4);
funccall();
funccall();
funccall();
funccall();
tracker.verify();

Output

/home/cg/root/63a002c52763b/main.js:3
const tracker = new assert.CallTracker();
   ^
   
TypeError: assert.CallTracker is not a constructor
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:17)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − Sometimes online compiler may not give us the expected result, so we are executing the above code locally.

If we compile and run the below code, the tracker.verify() function doesn't throw any error.

// Returns nothing
nodejs_assert_module.htm
Advertisements