Mock Functions
Mock functions are also known as "spies", because they let you spy on the behavior of a function that is called indirectly by some other code, rather than only testing the output. You can create a mock function with jest.fn(). If no implementation is given, the mock function will return undefined when invoked.
The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs:
import {expect, jest, test} from '@jest/globals';
Consult the Getting Started guide for details on how to setup Jest with TypeScript.
Methods
- Reference
mockFn.getMockImplementation()mockFn.getMockName()mockFn.mock.callsmockFn.mock.resultsmockFn.mock.instancesmockFn.mock.contextsmockFn.mock.lastCallmockFn.mockClear()mockFn.mockReset()mockFn.mockRestore()mockFn.mockImplementation(fn)mockFn.mockImplementationOnce(fn)mockFn.mockName(name)mockFn.mockReturnThis()mockFn.mockReturnValue(value)mockFn.mockReturnValueOnce(value)mockFn.mockResolvedValue(value)mockFn.mockResolvedValueOnce(value)mockFn.mockRejectedValue(value)mockFn.mockRejectedValueOnce(value)mockFn.withImplementation(fn, callback)
- Replaced Properties
- TypeScript Usage
Reference
mockFn.getMockImplementation()
Returns the current implementation of the mock function set by mockImplementation(). Returns undefined if no implementation has been set.
- JavaScript
- TypeScript
const mockFn = jest.fn();
mockFn.getMockImplementation(); // undefined
mockFn.mockImplementation(() => 42);
mockFn.getMockImplementation(); // () => 42
import {jest} from '@jest/globals';
const mockFn = jest.fn<() => number>();
mockFn.getMockImplementation(); // undefined
mockFn.mockImplementation(() => 42);
mockFn.getMockImplementation(); // () => 42
mockFn.getMockName()
Returns the mock name string set by calling .mockName().
mockFn.mock.calls
An array containing the call arguments of all calls that have been made to this mock function. Each item in the array is an array of arguments that were passed during the call.
For example: A mock function f that has been called twice, with the arguments f('arg1', 'arg2'), and then with the arguments f('arg3', 'arg4'), would have a mock.calls array that looks like this:
[
['arg1', 'arg2'],
['arg3', 'arg4'],
];
mockFn.mock.results
An array containing the results of all calls that have been made to this mock function. Each entry in this array is an object containing a type property, and a value property. type will be one of the following:
'return'- Indicates that the call completed by returning normally.'throw'- Indicates that the call completed by throwing a value.'incomplete'- Indicates that the call has not yet completed. This occurs if you test the result from within the mock function itself, or from within a function that was called by the mock.
The value property contains the value that was thrown or returned. value is undefined when type === 'incomplete'.
For example: A mock function f that has been called three times, returning 'result1', throwing an error, and then returning 'result2', would have a mock.results array that looks like this:
[
{
type: 'return',
value: 'result1',
},
{
type: 'throw',
value: {
/* Error instance */
},
},
{
type: 'return',
value: 'result2',
},
];
mockFn.mock.instances
An array that contains all the object instances that have been instantiated from this mock function using new.
For example: A mock function that has been instantiated twice would have the following mock.instances array:
const mockFn = jest.fn();
const a = new mockFn();
const b = new mockFn();
mockFn.mock.instances[0] === a; // true
mockFn.mock.instances[1] === b; // true