Function spy

  • Creates a spy function.

    Type Parameters

    • Self = any

      The self type of the function.

    • Args extends unknown[] = any[]

      The arguments type of the function.

    • Return = undefined

      The return type of the function.

    Returns Spy<Self, Args, Return>

    The spy function.

    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] });
  • Create a spy function with the given implementation.

    Type Parameters

    • Self

      The self type of the function to wrap

    • Args extends unknown[]

      The arguments type of the function to wrap

    • Return

      The return type of the function to wrap

    Parameters

    Returns Spy<Self, Args, Return>

    The wrapped function.

    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 });
  • Create a spy constructor.

    Type Parameters

    • Self

      The type of the instance of the class.

    • Args extends unknown[]

      The arguments type of the constructor

    Parameters

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

      The constructor to spy.

    Returns ConstructorSpy<Self, Args>

    The wrapped constructor.

    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"] });
  • Wraps a instance method with a Spy.

    Type Parameters

    • Self

      The type of the instance to spy the method of.

    • Prop extends string | number | symbol

      The property to spy.

    Parameters

    • self: Self

      The instance to spy.

    • property: Prop

      The property of the method to spy.

    Returns MethodSpy<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>

    The spy function.

    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 });