The wrapper function is expected to be called exactly exact
times. If the
function has not been called exactly exact
times when tracker.verify()
is called, then tracker.verify()
will throw an
error.
import assert from 'node:assert';
// Creates call tracker.
const tracker = new assert.CallTracker();
function func() {}
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func);
Optional
exact: numberA function that wraps fn
.
The wrapper function is expected to be called exactly exact
times. If the
function has not been called exactly exact
times when tracker.verify()
is called, then tracker.verify()
will throw an
error.
import assert from 'node:assert';
// Creates call tracker.
const tracker = new assert.CallTracker();
function func() {}
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func);
Optional
fn: FuncOptional
exact: numberA function that wraps fn
.
Example:
import assert from 'node:assert';
const tracker = new assert.CallTracker();
function func() {}
const callsfunc = tracker.calls(func);
callsfunc(1, 2, 3);
assert.deepStrictEqual(tracker.getCalls(callsfunc),
[{ thisArg: undefined, arguments: [1, 2, 3] }]);
An array with all the calls to a tracked function.
The arrays contains information about the expected and actual number of calls of the functions that have not been called the expected number of times.
import assert from 'node:assert';
// Creates call tracker.
const tracker = new assert.CallTracker();
function func() {}
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);
// Returns an array containing information on callsfunc()
console.log(tracker.report());
// [
// {
// message: 'Expected the func function to be executed 2 time(s) but was
// executed 0 time(s).',
// actual: 0,
// expected: 2,
// operator: 'func',
// stack: stack trace
// }
// ]
An array of objects containing information about the wrapper functions returned by tracker.calls().
Reset calls of the call tracker. If a tracked function is passed as an argument, the calls will be reset for it. If no arguments are passed, all tracked functions will be reset.
import assert from 'node:assert';
const tracker = new assert.CallTracker();
function func() {}
const callsfunc = tracker.calls(func);
callsfunc();
// Tracker was called once
assert.strictEqual(tracker.getCalls(callsfunc).length, 1);
tracker.reset(callsfunc);
assert.strictEqual(tracker.getCalls(callsfunc).length, 0);
Optional
fn: Functiona tracked function to reset.
Iterates through the list of functions passed to tracker.calls() and will throw an error for functions that have not been called the expected number of times.
import assert from 'node:assert';
// Creates call tracker.
const tracker = new assert.CallTracker();
function func() {}
// Returns a function that wraps func() that must be called exact times
// before tracker.verify().
const callsfunc = tracker.calls(func, 2);
callsfunc();
// Will throw an error since callsfunc() was only called once.
tracker.verify();
This feature is deprecated and will be removed in a future version. Please consider using alternatives such as the
mock
helper function.Since
v14.2.0, v12.19.0
Deprecated
Deprecated