• Creates a mock function similar to jest.fn but also tracks calls to object[methodName]

    Note: By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries.

    Type Parameters

    Parameters

    • object: T
    • method: Key
    • accessType: A

    Returns A extends SetAccessor
        ? SpyInstance<void, [Value]>
        : A extends GetAccessor
            ? SpyInstance<Value, []>
            : Value extends Constructor
                ? SpyInstance<InstanceType<Value>, ConstructorArgsType<Value>>
                : Value extends Func
                    ? SpyInstance<ReturnType<Value>, ArgsType<Value>>
                    : never

    const video = require('./video');

    test('plays video', () => {
    const spy = jest.spyOn(video, 'play');
    const isPlaying = video.play();

    expect(spy).toHaveBeenCalled();
    expect(isPlaying).toBe(true);

    spy.mockReset();
    spy.mockRestore();
    });
  • Creates a mock function similar to jest.fn but also tracks calls to object[methodName]

    Note: By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries.

    Type Parameters

    • T extends {}
    • M extends string | number | symbol

    Parameters

    • object: T
    • method: M

    Returns ConstructorProperties<Required<T>>[M] extends new (...args: any[]) => any
        ? SpyInstance<
            InstanceType<ConstructorProperties<Required<T>>[M]>,
            ConstructorArgsType<ConstructorProperties<Required<T>>[M]>,
        >
        : never

    const video = require('./video');

    test('plays video', () => {
    const spy = jest.spyOn(video, 'play');
    const isPlaying = video.play();

    expect(spy).toHaveBeenCalled();
    expect(isPlaying).toBe(true);

    spy.mockReset();
    spy.mockRestore();
    });
  • Creates a mock function similar to jest.fn but also tracks calls to object[methodName]

    Note: By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries.

    Type Parameters

    • T extends {}
    • M extends string | number | symbol

    Parameters

    • object: T
    • method: M

    Returns FunctionProperties<Required<T>>[M] extends Func
        ? SpyInstance<
            ReturnType<FunctionProperties<Required<T>>[M]>,
            ArgsType<FunctionProperties<Required<T>>[M]>,
        >
        : never

    const video = require('./video');

    test('plays video', () => {
    const spy = jest.spyOn(video, 'play');
    const isPlaying = video.play();

    expect(spy).toHaveBeenCalled();
    expect(isPlaying).toBe(true);

    spy.mockReset();
    spy.mockRestore();
    });