This feature is deprecated and will be removed in a future version. Please consider using alternatives such as the mock helper function.

v14.2.0, v12.19.0

Deprecated

Constructors

Methods

  • 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);

    Parameters

    • Optionalexact: number

    Returns () => void

    A function that wraps fn.

    v14.2.0, v12.19.0

  • 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);

    Type Parameters

    • Func extends (...args: any[]) => any

    Parameters

    • Optionalfn: Func
    • Optionalexact: number

    Returns Func

    A function that wraps fn.

    v14.2.0, v12.19.0

  • 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] }]);

    Parameters

    • fn: Function

    Returns assert.CallTrackerCall[]

    An array with all the calls to a tracked function.

    v18.8.0, v16.18.0

  • 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
    // }
    // ]

    Returns assert.CallTrackerReportInformation[]

    An array of objects containing information about the wrapper functions returned by tracker.calls().

    v14.2.0, v12.19.0

  • 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);

    Parameters

    • Optionalfn: Function

      a tracked function to reset.

    Returns void

    v18.8.0, v16.18.0

  • 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();

    Returns void

    v14.2.0, v12.19.0