Spy

import { Spy } from "jsr:@ende/testing/mock";

interface

interface Spy<Self = any, Args extends unknown[] = any[], Return = any>

A function or instance method wrapper that records all calls made to it.

Call signatures

Call signature

(this: Self, ...args: Args): Return

Properties

original

original: (this: Self, ...args: Args) => Return

The function that is being spied on.

calls

calls: SpyCall<Self, Args, Return>[]

Information about calls made to the function or instance method.

restored

restored: boolean

Whether or not the original instance method has been restored.

Methods

restore

restore(): void

If spying on an instance method, this restores the original instance method.

Source

spy

import { spy } from "jsr:@ende/testing/mock";

function

function spy<Self = any, Args extends unknown[] = any[], Return = undefined>(): Spy<Self, Args, Return>

Creates a spy function.

Returns

The spy function.

Example: Usage

import {
  assertSpyCall,
  assertSpyCalls,
  spy,
} from "@ende/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] });

function

function spy<Self, Args extends unknown[], Return>(func: (this: Self, ...args: Args) => Return): Spy<Self, Args, Return>

Create a spy function with the given implementation.

func: (this: Self, ...args: Args) => Return

The function to wrap

Returns

The wrapped function.

Example: Usage

import {
  assertSpyCall,
  assertSpyCalls,
  spy,
} from "@ende/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 });

function

function spy<Self, Args extends unknown[]>(constructor: new (...args: Args) => Self): ConstructorSpy<Self, Args>

Create a spy constructor.

constructor: new (...args: Args) => Self

The constructor to spy.

Returns

The wrapped constructor.

Example: Usage

import {
  assertSpyCall,
  assertSpyCalls,
  spy,
} from "@ende/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"] });

function

function spy<Self, Prop extends keyof Self>(self: Self, property: Prop): MethodSpy<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>

Wraps a instance method with a Spy.

self: Self

The instance to spy.

property: Prop

The property of the method to spy.

Returns

The spy function.

Example: Usage

import {
  assertSpyCall,
  assertSpyCalls,
  spy,
} from "@ende/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 });

function

function spy<Self, Args extends unknown[], Return>(funcOrConstOrSelf?: ((this: Self, ...args: Args) => Return) | (new (...args: Args) => Self) | Self, property?: keyof Self): SpyLike<Self, Args, Return>

Source