interface MockWithArgs<T extends MockableFunction> {
    new MockWithArgs(...args: ConstructorArgumentsOf<T>): T;
    mock: MockContext<ReturnType<T>, ArgumentsOf<T>, ConstructorReturnType<T>>;
    getMockImplementation(): | undefined
    | (...args: ArgumentsOf<T>) => ReturnType<T>;
    getMockName(): string;
    mockClear(): this;
    mockImplementation(fn?: (...args: ArgumentsOf<T>) => ReturnType<T>): this;
    mockImplementationOnce(
        fn: (...args: ArgumentsOf<T>) => ReturnType<T>,
    ): this;
    mockName(name: string): this;
    mockRejectedValue(value: RejectedValue<ReturnType<T>>): this;
    mockRejectedValueOnce(value: RejectedValue<ReturnType<T>>): this;
    mockReset(): this;
    mockResolvedValue(value: ResolvedValue<ReturnType<T>>): this;
    mockResolvedValueOnce(value: ResolvedValue<ReturnType<T>>): this;
    mockRestore(): void;
    mockReturnThis(): this;
    mockReturnValue(value: ReturnType<T>): this;
    mockReturnValueOnce(value: ReturnType<T>): this;
    withImplementation(
        fn: (...args: ArgumentsOf<T>) => ReturnType<T>,
        callback: () => Promise<unknown>,
    ): Promise<void>;
    withImplementation(
        fn: (...args: ArgumentsOf<T>) => ReturnType<T>,
        callback: () => void,
    ): void;
    (...args: ArgumentsOf<T>): ReturnType<T>;
}

Type Parameters

Hierarchy (View Summary)

  • Parameters

    Returns ReturnType<T>

Constructors

  • Parameters

    Returns T

Properties

Provides access to the mock's metadata

Methods

  • Returns the function that was set as the implementation of the mock (using mockImplementation).

    Returns undefined | (...args: ArgumentsOf<T>) => ReturnType<T>

  • Returns the mock name string set by calling mockFn.mockName(value).

    Returns string

  • Resets all information stored in the mockFn.mock.calls and mockFn.mock.instances arrays.

    Often this is useful when you want to clean up a mock's usage data between two assertions.

    Beware that mockClear will replace mockFn.mock, not just mockFn.mock.calls and mockFn.mock.instances. You should therefore avoid assigning mockFn.mock to other variables, temporary or not, to make sure you don't access stale data.

    Returns this

  • Accepts a function that should be used as the implementation of the mock. The mock itself will still record all calls that go into and instances that come from itself – the only difference is that the implementation will also be executed when the mock is called.

    Note: jest.fn(implementation) is a shorthand for jest.fn().mockImplementation(implementation).

    Parameters

    Returns this

  • Accepts a function that will be used as an implementation of the mock for one call to the mocked function. Can be chained so that multiple function calls produce different results.

    Parameters

    Returns this

    const myMockFn = jest
    .fn()
    .mockImplementationOnce(cb => cb(null, true))
    .mockImplementationOnce(cb => cb(null, false));

    myMockFn((err, val) => console.log(val)); // true

    myMockFn((err, val) => console.log(val)); // false
  • Sets the name of the mock.

    Parameters

    • name: string

    Returns this

  • Simple sugar function for: jest.fn().mockImplementation(() => Promise.reject(value));

    Parameters

    Returns this

    test('async test', async () => {
    const asyncMock = jest.fn().mockRejectedValue(new Error('Async error'));

    await asyncMock(); // throws "Async error"
    });
  • Simple sugar function for: jest.fn().mockImplementationOnce(() => Promise.reject(value));

    Parameters

    Returns this

    test('async test', async () => {
    const asyncMock = jest
    .fn()
    .mockResolvedValueOnce('first call')
    .mockRejectedValueOnce(new Error('Async error'));

    await asyncMock(); // first call
    await asyncMock(); // throws "Async error"
    });
  • Resets all information stored in the mock, including any initial implementation and mock name given.

    This is useful when you want to completely restore a mock back to its initial state.

    Beware that mockReset will replace mockFn.mock, not just mockFn.mock.calls and mockFn.mock.instances. You should therefore avoid assigning mockFn.mock to other variables, temporary or not, to make sure you don't access stale data.

    Returns this

  • Simple sugar function for: jest.fn().mockImplementation(() => Promise.resolve(value));

    Parameters

    Returns this

  • Simple sugar function for: jest.fn().mockImplementationOnce(() => Promise.resolve(value));

    Parameters

    Returns this

    test('async test', async () => {
    const asyncMock = jest
    .fn()
    .mockResolvedValue('default')
    .mockResolvedValueOnce('first call')
    .mockResolvedValueOnce('second call');

    await asyncMock(); // first call
    await asyncMock(); // second call
    await asyncMock(); // default
    await asyncMock(); // default
    });
  • Does everything that mockFn.mockReset() does, and also restores the original (non-mocked) implementation.

    This is useful when you want to mock functions in certain test cases and restore the original implementation in others.

    Beware that mockFn.mockRestore only works when mock was created with jest.spyOn. Thus you have to take care of restoration yourself when manually assigning jest.fn().

    The restoreMocks configuration option is available to restore mocks automatically between tests.

    Returns void

  • Just a simple sugar function for:

    Returns this

    jest.fn(function() {
    return this;
    });
  • Accepts a value that will be returned whenever the mock function is called.

    Parameters

    • value: ReturnType<T>

    Returns this

    const mock = jest.fn();
    mock.mockReturnValue(42);
    mock(); // 42
    mock.mockReturnValue(43);
    mock(); // 43
  • Accepts a value that will be returned for one call to the mock function. Can be chained so that successive calls to the mock function return different values. When there are no more mockReturnValueOnce values to use, calls will return a value specified by mockReturnValue.

    Parameters

    • value: ReturnType<T>

    Returns this

    const myMockFn = jest.fn()
    .mockReturnValue('default')
    .mockReturnValueOnce('first call')
    .mockReturnValueOnce('second call');

    // 'first call', 'second call', 'default', 'default'
    console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
  • Temporarily overrides the default mock implementation within the callback, then restores its previous implementation.

    Parameters

    • fn: (...args: ArgumentsOf<T>) => ReturnType<T>
    • callback: () => Promise<unknown>

    Returns Promise<void>

    If the callback is async or returns a thenable, withImplementation will return a promise. Awaiting the promise will await the callback and reset the implementation.

  • Temporarily overrides the default mock implementation within the callback, then restores its previous implementation.

    Parameters

    • fn: (...args: ArgumentsOf<T>) => ReturnType<T>
    • callback: () => void

    Returns void