Open In App

Node.js assert tracker.calls() Function

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:

Return Value: A wrapper function that wraps fn.



Example 1:




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:




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


Article Tags :