Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

Callable

  • Spy(...args: any[]): any
  • Used to provide custom behaviors at test time to control code flow and ensure code coverage. Can also be used to verify individual call data (args and context) as well as overall invocation counts.

    example
    import { test } from '@paychex/core';

    const method = test.spy(); // create a new Spy instance

    method.returns(123); // default behavior
    method.onCall(1).throws(new Error()); // throw error on 2nd call

    method.called; // false

    method(); // 123

    method.called; // true
    method.callCount; // 1

    try {
    method();
    } catch (e) {
    // 2nd invocation throws error
    }

    method.call({}, 'abc');
    method.callCount; // 3

    method.calls(2).args; // ['abc']
    method.calls(2).context; // {}
    method.calls(2).callTime; // Date

    method('def');
    method.calls.mostRecent().args; // ['def']

    // invoke a different function when the
    // spy is called
    method.invokes(function(...args) {
    console.log('proxy method called', args);
    });

    method('abc', 123); // "proxy method called ['abc', 123]"

    method.reset();
    method.called; // false
    method.callCount; // 0

    method(); // undefined

    Parameters

    • Rest ...args: any[]

    Returns any

Index

Properties

args: any[]

The arguments passed to this invocation of the spy.

callCount: number

The number of times the spy was invoked.

callTime: Date

When the spy was invoked.

called: boolean

true if the spy was invoked.

calls: SpyCall[]

Collection of data about each call.

context: any

The this context used for this invocation of the spy.

Methods

  • invokes(fn: Function): Spy
  • Calls the specified method when the {@link Spy} is invoked.

    example
    import { spy } from '@paychex/core';

    const method = spy().invokes(function(...args) {
    console.log('method called with', args);
    });

    method.onCall(1).invokes(function(...args) {
    console.log('method called 2nd time', args);
    });

    Parameters

    • fn: Function

    Returns Spy

    The original context, for chaining.

  • Changes the behaviors for a specific invocation of the Spy.

    example
    import { spy } from '@paychex/core';

    const method = spy().returns(123);
    method.onCall(2).returns(456);
    method.onCall(3).invokes(console.log);
    method.onCall(4).throws(new Error());

    method(); // 123
    method(); // 123
    method(); // 456
    method('hello'); // "hello"
    method(); // throws

    Parameters

    • index: number

      The index of the call whose behavior should be changed.

    Returns SpyBehavior

    The possible behaviors to invoke for this call.

  • reset(): void
  • Returns all calls to their default behaviors and clears call history.

    Returns void

  • returns(value: any): Spy
  • Returns the specified value when the {@link Spy} is invoked.

    example
    import { spy } from '@paychex/core';

    const method = spy().returns('abc');

    method.onCall(1).returns('def');

    Parameters

    • value: any

      The value to return.

    Returns Spy

    The original context, for chaining.

  • throws(err: Error): Spy
  • Throws the specified value when the {@link Spy} is invoked.

    example
    import { spy } from '@paychex/core';

    const method = spy().throws(new Error());

    method.onCall(1).throws(new Error('2nd call'));

    Parameters

    • err: Error

    Returns Spy

    The original context, for chaining.