Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Mock<T, Y>

Type parameters

  • T

  • Y: any[]

Hierarchy

Callable

  • __call(...args: Y): T
  • Parameters

    • Rest ...args: Y

    Returns T

Index

Constructors

constructor

  • new Mock(...args: Y): T
  • Parameters

    • Rest ...args: Y

    Returns T

Properties

Function

arguments

arguments: any

caller

caller: Function

length

length: number

mock

mock: MockContext<T, Y>

Provides access to the mock's metadata

name

name: string

Returns the name of the function. Function names are read-only and can not be changed.

prototype

prototype: any

Methods

[Symbol.hasInstance]

  • [Symbol.hasInstance](value: any): boolean
  • Determines whether the given value inherits from this function if this function was used as a constructor function.

    A constructor function can control which objects are recognized as its instances by 'instanceof' by overriding this method.

    Parameters

    • value: any

    Returns boolean

apply

  • apply(this: Function, thisArg: any, argArray?: any): any
  • Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.

    Parameters

    • this: Function
    • thisArg: any

      The object to be used as the this object.

    • Optional argArray: any

      A set of arguments to be passed to the function.

    Returns any

bind

  • bind(this: Function, thisArg: any, ...argArray: any[]): any
  • For a given function, creates a bound function that has the same body as the original function. The this object of the bound function is associated with the specified object, and has the specified initial parameters.

    Parameters

    • this: Function
    • thisArg: any

      An object to which the this keyword can refer inside the new function.

    • Rest ...argArray: any[]

      A list of arguments to be passed to the new function.

    Returns any

call

  • call(this: Function, thisArg: any, ...argArray: any[]): any
  • Calls a method of an object, substituting another object for the current object.

    Parameters

    • this: Function
    • thisArg: any

      The object to be used as the current object.

    • Rest ...argArray: any[]

      A list of arguments to be passed to the method.

    Returns any

getMockName

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

    Returns string

mockClear

  • mockClear(): void
  • 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 void

mockImplementation

  • mockImplementation(fn?: undefined | ((...args: Y) => T)): 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

    • Optional fn: undefined | ((...args: Y) => T)

    Returns this

mockImplementationOnce

  • mockImplementationOnce(fn: (...args: Y) => T): 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.

    example

    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

    Parameters

    • fn: (...args: Y) => T
        • (...args: Y): T
        • Parameters

          • Rest ...args: Y

          Returns T

    Returns this

mockName

  • mockName(name: string): this
  • Sets the name of the mock`.

    Parameters

    • name: string

    Returns this

mockRejectedValue

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

    example

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

    await asyncMock(); // throws "Async error" });

    Parameters

    Returns this

mockRejectedValueOnce

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

    example

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

    Parameters

    Returns this

mockReset

  • mockReset(): void
  • 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 void

mockResolvedValue

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

    Parameters

    Returns this

mockResolvedValueOnce

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

    example

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

    Parameters

    Returns this

mockRestore

  • mockRestore(): void
  • 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

mockReturnThis

  • mockReturnThis(): this
  • Just a simple sugar function for:

    example

    jest.fn(function() { return this; });

    Returns this

mockReturnValue

  • mockReturnValue(value: T): this
  • Accepts a value that will be returned whenever the mock function is called.

    example

    const mock = jest.fn(); mock.mockReturnValue(42); mock(); // 42 mock.mockReturnValue(43); mock(); // 43

    Parameters

    • value: T

    Returns this

mockReturnValueOnce

  • mockReturnValueOnce(value: T): this
  • 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.

    example

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

    // 'first call', 'second call', 'default', 'default' console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());

    Parameters

    • value: T

    Returns this

toString

  • toString(): string
  • Returns a string representation of a function.

    Returns string