Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CallTracker

This feature is currently experimental and behavior might still change.

since

v14.2.0, v12.19.0

experimental

Hierarchy

  • CallTracker

Constructors

Methods

Constructors

constructor

Methods

calls

  • calls(exact?: number): function
  • calls<Func>(fn?: Func, exact?: number): Func
  • 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 '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);
    
    since

    v14.2.0, v12.19.0

    Parameters

    • Optional exact: number

    Returns function

    that wraps fn.

      • (): void
      • Returns void

  • Type parameters

    • Func: function

    Parameters

    • Optional fn: Func
    • Optional exact: number

    Returns Func

report

  • 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 'assert';
    
    // Creates call tracker.
    const tracker = new assert.CallTracker();
    
    function func() {}
    
    function foo() {}
    
    // 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()
    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
    //  }
    // ]
    
    since

    v14.2.0, v12.19.0

    Returns CallTrackerReportInformation[]

    of objects containing information about the wrapper functions returned by calls.

verify

  • verify(): void
  • 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 '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();
    
    since

    v14.2.0, v12.19.0

    Returns void

Generated using TypeDoc