Introduction
Mocking is a technique in unit testing in which the external dependencies of the code being tested are replaced by dummy implementations so that the code to be tested can be controlled and test runs can be more quick and reliable. In simple words, Mocking or Mock implementation is creating a dummy version of an external or internal service on which the code to be tested depends.
Mocking in Jest is generally replacing dependencies with mock functions. There are different ways for mock implementation in Jest, but before moving to those, let’s first discuss what mock functions are.
Mock Function
Mock functions are used for mock implementation. These functions are also called ‘spies’ because they can be used to spy on the behaviour of a function that is called by the external code instead of just testing. Mock functions provide a way to replace the implementation details of external dependencies and allow us to produce unit tests that are focused, reproducible, and independent of external factors.
There are two different ways to mock functions. First, create a mock function that will be used in test code, and the second is to write a manual mock to override a manual dependency.




