Options
All
  • Public
  • Public/Protected
  • All
Menu

This class implements a "spy" - function wrapper which tracks its calls. Spy can be installed instead of a method in some object or can be used standalone.

Usually you don't use this class directly, but instead via the test helper methods: spyOn, createSpy, createSpyObj.

Spy can "operate" in several modes, called "strategies". See callThrough, callFake, stub, returnValue, throwError below. Default one is callThrough.

Note, that spies "belongs" to a test and once the test is completed all, spies that were installed during its execution will be removed.

Hierarchy

Index

Constructors

constructor

Properties

calls

calls: CallsLog = ...

This property contains CallsLog instance, with several helper methods, related to the calls tracking information. It is available as the property of the spy object and also assigned to the spies wrapper function.

Example:

const spy = t.spyOn(obj, 'someMethod').callThrough()

obj.someMethod(0, 1)
obj.someMethod(1, 2)

// available both on spy and wrapper
t.expect(spy.calls.any()).toBe(true)
t.expect(obj.someMethod.calls.count()).toBe(2)

t.expect(spy.calls.first()).toEqual({ object : obj, args : [ 0, 1 ], returnValue : undefined })

Accessors

and

  • get and(): this
  • This is just a reference to itself, to add some syntax sugar.

    This property is also assigned to the wrapper function of spy.

    t.spyOn(obj, 'someMethod').callThrough()

    // same thing as above
    t.spyOn(obj, 'someMethod').and.callThrough()

    // returns spy instance
    obj.someMethod.and

    Returns this

Methods

callFake

  • callFake(func: Function): Spy
  • This method makes the spy to call the provided function and return the value from it, instead of the original function.

    Parameters

    • func: Function

      The function to call instead of the original function

    Returns Spy

callThrough

  • callThrough(): Spy
  • This method makes the spy to also execute the original function it has been installed over. The value returned from original function is returned from the spy.

    Returns Spy

initialize

  • initialize(props?: Partial<Spy>): void
  • This method applies its 1st argument (if any) to the current instance using Object.assign().

    Supposed to be overridden in the subclasses to customize the instance creation process.

    Parameters

    • Optional props: Partial<Spy>

    Returns void

reset

  • reset(): void

returnValue

  • returnValue(value: unknown): Spy
  • This method makes the spy to return the value provided and not execute the original function.

    Parameters

    • value: unknown

      The value that will be returned from the spy.

    Returns Spy

stub

throwError

  • throwError(error: unknown): Spy
  • This method makes the spy to throw the specified error value (instead of calling the original function).

    Parameters

    • error: unknown

      The error value to throw. If it is not an Error instance, it will be passed to Error constructor first.

    Returns Spy

    This spy instance

Static maybeNew

  • maybeNew<T>(props?: InstanceType<T> | Partial<InstanceType<T>>): InstanceType<T>
  • This is a type-safe static constructor method, accepting a single argument. If that argument is already an instance of this class - it is returned right away, otherwise the new constructor is used for instantiation.

    Type parameters

    Parameters

    • Optional props: InstanceType<T> | Partial<InstanceType<T>>

    Returns InstanceType<T>

Static new

  • new<T>(props?: Partial<InstanceType<T>>): InstanceType<T>
  • This is a type-safe static constructor method, accepting a single argument, with the object, corresponding to the class properties. It will generate a compilation error, if unknown property is provided.

    For example:

    class MyClass extends Base {
    prop : string
    }

    const instance : MyClass = MyClass.new({ prop : 'prop', wrong : 11 })

    will produce:

    TS2345: Argument of type '{ prop: string; wrong: number; }' is not assignable to parameter of type 'Partial<MyClass>'.
    Object literal may only specify known properties, and 'wrong' does not exist in type 'Partial<MyClass>'

    The only thing this constructor does is create an instance and call the initialize method on it, forwarding the first argument. The customization of instance is supposed to be performed in that method.

    Type parameters

    Parameters

    • Optional props: Partial<InstanceType<T>>

    Returns InstanceType<T>

Generated using TypeDoc