Jest mockFn.mockImplementation(fn) function

Posted by Andre Scott | Updated on

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).

For example:

mockImplementation can also be used to mock class constructors:

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@] Your question and answer will appear on and help other developers.

Related Questions & Answers