namespace Spy
Contains functions that can be used to Spy, Stub, and partially Mock functions, objects and classes.
Interfaces
Name | Description |
---|---|
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.
cls
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
Name | Description |
---|---|
I | the class instance type |
A | the constructor parameter types |
Parameters
Name | Type | Description |
---|---|---|
originalClass | Construct <A, I> | (optional) the class to spy on |
originalStubs | Partial<I> | (optional) the default stubs to use for each created instance |
originalConstructorStub | Func <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.
fn
Definition
function fn<F extends
Func
>(originalFn?: F, originalStub?:
Spy.FnStub
<F>, onCall?: (args: Parameters<F>) => void):
Spy.Fn
<F>;
Type parameters
Name | Description |
---|---|
F | the type of function to spy on |
Parameters
Name | Type | Description |
---|---|---|
originalFn | F | (optional) the original function to spy on, if stubbed still useful to supply to get the correct types |
originalStub | Spy.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 |
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.
obj
Definition
function obj<T extends {
readonly [key: string
|
number
|
symbol]: any;
}>(originalObj?: T, stubs?:
Spy.ObjStub
<T>):
Spy.Obj
<T>;
Type parameters
Name | Description |
---|---|
T | the object type to spy on |
Parameters
Name | Type | Description |
---|---|---|
originalObj | T | (optional) the original object to spy on |
stubs | Spy.ObjStub <T> | (optional) a partial implementation of the object type containing default stubs |
Constants
Name | Description |
---|---|
META | undocumented |