Skip to main content

namespace Spy

Contains functions that can be used to Spy, Stub, and partially Mock functions, objects and classes.

Interfaces

NameDescription
Spy.ClsMeta<C,A>The metadata object type for a spied class.
Spy.ClsObjMeta<T,A>undocumented
Spy.Fn<F>The spied function type containing extra information about the function calls.
Spy.FnMeta<F>The extra information added to spied functions.
Spy.ObjMeta<T>The additional information available for spied objects.

Functions

cls

Returns a tuple containing a spied class and an object containing extra information about the created class instances.

Definition

function cls<I, A extends any[] = I extends Construct? ConstructorParameters<I> : []>(originalClass?:Construct<A, I>, originalStubs?: Partial<I>, originalConstructorStub?: Func<A, I>): Cls<I, A>;

Type parameters
NameDescription
Ithe class instance type
Athe constructor parameter types

Parameters

NameTypeDescription
originalClassConstruct<A, I>(optional) the class to spy on
originalStubsPartial<I>(optional) the default stubs to use for each created instance
originalConstructorStubFunc<A, I>(optional) a function to call instead of the class constructor

fn

Returns a spied function instance that tracks the function calls and optionally uses some original or stub implementation.

Definition

function fn<F extends Func>(originalFn?: F, originalStub?: Spy.FnStub<F>, onCall?: (args: Parameters<F>) => void): Spy.Fn<F>;

Type parameters
NameDescription
Fthe type of function to spy on

Parameters

NameTypeDescription
originalFnF(optional) the original function to spy on, if stubbed still useful to supply to get the correct types
originalStubSpy.FnStub<F>(optional) the default stub implementation to use when the function is called
onCall(args: Parameters<F>) => void(optional) a callback function that receives the parameters used on each function call
example
function f(x: number, y: number) {
return x + y;
}
const spy = Spy.fn(f, (x) => -x);
spy(4, 5);
// => -4
spy.nrCalls;
// => 1
spy.calls;
// => [[4, 5]]

obj

Returns a tuple containing the spied object, and the metadata object giving more information about the stubbed object.

Definition

function obj<T extends {
    readonly [key: string |number| symbol]: any;
  }>(originalObj?: T, stubs?: Spy.ObjStub<T>): Spy.Obj<T>;

Type parameters
NameDescription
Tthe object type to spy on

Parameters

NameTypeDescription
originalObjT(optional) the original object to spy on
stubsSpy.ObjStub<T>(optional) a partial implementation of the object type containing default stubs

Constants

NameDescription
METAundocumented