Open In App

Node.js assert tracker.calls() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The tracker.calls() method is used to keep track of the number of times a function is executed. It returns a wrapper function that should be invoked at exact times. When tracker.verify() is executed, if the method has not been called precisely exact times, tracker.verify() will throw an exception.

Syntax:

tracker.calls([fn][, exact])

Parameters:

  • fn: The function to be monitored. A no-op function is the default value.
  • exact (number): The number of times. Its default value is 1.

Return Value: A wrapper function that wraps fn.

Example 1:

Javascript




import assert from 'node:assert';
  
const tracker = new assert.CallTracker();
  
function func() { console.log("Hello World") };
  
const callsfunc = tracker.calls(func, 2);
  
callsfunc();
callsfunc();
  
process.on('exit', () => {
    tracker.verify()
});


Output:

Hello World
Hello World

Example 2:

Javascript




import assert from 'node:assert';
  
const tracker = new assert.CallTracker();
  
function func() { console.log("Hello World") };
  
const callsfunc = tracker.calls(func, 2);
  
callsfunc();
  
process.on('exit', () => {
    tracker.verify()
});


Output:

 

Reference: https://nodejs.org/api/assert.html#trackercallsfn-exact



Last Updated : 14 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads