MockInstance
MockInstance is the type of all mock/spy instances, providing a rich set of APIs for controlling and inspecting mocks.
getMockName
- Type:
() => string
Returns the mock name string set by .mockName().
mockName
- Type:
(name: string) => MockInstance
Sets the mock name for this mock instance, useful for debugging and output.
mockClear
- Type:
() => MockInstance
Clears all information about every call (calls, instances, contexts, results, etc.).
mockReset
- Type:
() => MockInstance
Clears all call information and resets the implementation to the initial state.
mockRestore
- Type:
() => MockInstance
Restores the original method of a spied object (only effective for spies).
getMockImplementation
- Type:
() => Function | undefined
Returns the current mock implementation function, if any.
mockImplementation
- Type:
(fn: Function) => MockInstance
Sets the implementation function for the mock.
mockImplementationOnce
- Type:
(fn: Function) => MockInstance
Sets the implementation function for the next call only.
withImplementation
- Type:
(fn: Function, callback: () => any) => void | Promise<void>
Temporarily replaces the mock implementation while the callback is executed, then restores the original implementation.
mockReturnThis
- Type:
() => this
Makes the mock return this when called.
mockReturnValue
- Type:
(value: any) => MockInstance
Makes the mock always return the specified value.
mockReturnValueOnce
- Type:
(value: any) => MockInstance
Makes the mock return the specified value for the next call only.
mockResolvedValue
- Type:
(value: any) => MockInstance
Makes the mock return a Promise that resolves to the specified value.
mockResolvedValueOnce
- Type:
(value: any) => MockInstance
Makes the mock return a Promise that resolves to the specified value for the next call only.
mockRejectedValue
- Type:
(error: any) => MockInstance
Makes the mock return a Promise that rejects with the specified error.
mockRejectedValueOnce
- Type:
(error: any) => MockInstance
Makes the mock return a Promise that rejects with the specified error for the next call only.
mock
The context of the mock, including call arguments, return values, instances, contexts, etc.
mock.calls
- Type:
Array<Parameters<T>>
An array containing the arguments for each call to the mock function.
mock.instances
- Type:
Array<ReturnType<T>>
An array containing the instances that have been instantiated from the mock (when used as a constructor).
mock.contexts
- Type:
Array<ReturnType<T>>
An array containing the this context for each call to the mock function.
mock.invocationCallOrder
- Type:
Array<number>
An array of numbers representing the order in which the mock was called, shared across all mocks. The index starts from 1.
mock.lastCall
- Type:
Parameters<T> | undefined
The arguments of the last call to the mock function, or undefined if it has not been called.
mock.results
- Type:
Array<MockResult<ReturnType<T>>>
An array containing the result of each call to the mock function, including returned values, thrown errors, or incomplete calls.
mock.settledResults
- Type:
Array<MockSettledResult<Awaited<ReturnType<T>>>>
An array containing the settled results (fulfilled or rejected) of all async calls to the mock function.