
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
Node.js - assert.report() Function
The assert module provides a set of assertion functions for verifying invariants. The tracker.report() function is an inbuilt function of the assert module of Node.js.
The Node.js tracker.report() function is helpful when we want to know the information about the expected and actual number of calls of the functions that have not been called the expected number of times.
This function returns an array of objects which contains all the information about the wrapper function returned by the tracker.calls() function.
Syntax
Following is the example of the Node.js tracker.report() function −
tracker.report();
Parameters
This function doesn't accept any parameters.
Return Value
The function tracker.report() returns an array of objects holding information about the wrapper function returned by tracker.calls() function.
The information object holding is described below −
message<string> − The message will be assigned by the function automatically.
actual<number> − This shows the actual number of times the function was called.
expected<number> − This shows the number of times the function was expected to call.
operator<operator> − This will let us know the name of the function that is wrapped by the wrapper function.
stack<Object> − A stack trace of the function.
Example
In the following example,
We are creating a call tracker object.
Then we are creating a wrapped function.
Then we are passing the function func as a parameter to the tracker.calls() function we wrapped the function func to the wrapper function funccall.
Then we are calling the tracker.report() function.
const assert = require('assert'); const tracker = new assert.CallTracker(); function func() {}; const callsfunc = tracker.calls(func, 5); console.log(tracker.report());
Output
/home/cg/root/63a002c52763b/main.js:1 (function (exports, require, module, __filename, __dirname) { aconst assert = require('assert'); ^^^^^^ SyntaxError: Unexpected identifier at new Script (vm.js:74:7) at createScript (vm.js:246:10) at Object.runInThisContext (vm.js:298:10) at Module._compile (internal/modules/cjs/loader.js:670:28) 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)
Note − Sometimes online compiler may not give us the expected result, so we are executing the above code locally.
When we compile and run the code, the function tracker.report() will display an array of objects in the output.
[ { message: 'Expected the func function to be executed 5 time(s) but was executed 0 time(s).', actual: 0, expected: 5, operator: 'func', stack: Error at CallTracker.calls (node:internal/assert/calltracker:44:19) at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:7:27) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 } ]
Example
In the example,
We are doing everything the same as the above program.
But in this code, we are calling the wrapper function 3 times, which is not matching with the number we passed to the exact parameter of tracker.calls() function.
const assert = require('assert'); const tracker = new assert.CallTracker(); function func() {}; const callsfunc = tracker.calls(func, 5); callsfunc(); callsfunc(); callsfunc(); console.log(tracker.report());
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 code, tracker.reports() function will display an array of objects in the output.
[ { message: 'Expected the func function to be executed 5 time(s) but was executed 3 time(s).', actual: 3, expected: 5, operator: 'func', stack: Error at CallTracker.calls (node:internal/assert/calltracker:44:19) at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:7:27) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 } ]
Example
In the following example below, we are calling the wrapper function as expected to the number we passed in the exact parameter of the tracker.calls() function.
const assert = require('assert'); const tracker = new assert.CallTracker(); function func() {}; const callsfunc = tracker.calls(func, 5); callsfunc(); callsfunc(); callsfunc(); callsfunc(); callsfunc(); console.log(tracker.report());
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.
So, if we compile and run the code, the tracker.report() function will return an empty array of objects.
[]