Function spy
- spy<Self, Args, Return>(): Spy<Self, Args, Return>
Type Parameters
Returns Spy<Self, Args, Return>
The spy function.
Example: Usage
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "@std/testing/mock";
const func = spy();
func();
func(1);
func(2, 3);
assertSpyCalls(func, 3);
// asserts each call made to the spy function.
assertSpyCall(func, 0, { args: [] });
assertSpyCall(func, 1, { args: [1] });
assertSpyCall(func, 2, { args: [2, 3] });- spy<Self, Args, Return>(func): Spy<Self, Args, Return>
Create a spy function with the given implementation.
Type Parameters
Parameters
Returns Spy<Self, Args, Return>
The wrapped function.
Example: Usage
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "@std/testing/mock";
const func = spy((a: number, b: number) => a + b);
func(3, 4);
func(5, 6);
assertSpyCalls(func, 2);
// asserts each call made to the spy function.
assertSpyCall(func, 0, { args: [3, 4], returned: 7 });
assertSpyCall(func, 1, { args: [5, 6], returned: 11 });- spy<Self, Args>(constructor): ConstructorSpy<Self, Args>
Create a spy constructor.
Type Parameters
Parameters
Returns ConstructorSpy<Self, Args>
The wrapped constructor.
Example: Usage
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "@std/testing/mock";
class Foo {
constructor(value: string) {}
};
const Constructor = spy(Foo);
new Constructor("foo");
new Constructor("bar");
assertSpyCalls(Constructor, 2);
// asserts each call made to the spy function.
assertSpyCall(Constructor, 0, { args: ["foo"] });
assertSpyCall(Constructor, 1, { args: ["bar"] });- spy<Self, Prop>(self, property): MethodSpy<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>
Wraps a instance method with a Spy.
Type Parameters
Returns MethodSpy<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>
The spy function.
Example: Usage
import {
assertSpyCall,
assertSpyCalls,
spy,
} from "@std/testing/mock";
const obj = {
method(a: number, b: number): number {
return a + b;
},
};
const methodSpy = spy(obj, "method");
obj.method(1, 2);
obj.method(3, 4);
assertSpyCalls(methodSpy, 2);
// asserts each call made to the spy function.
assertSpyCall(methodSpy, 0, { args: [1, 2], returned: 3 });
assertSpyCall(methodSpy, 1, { args: [3, 4], returned: 7 });
Creates a spy function.