Introduction to Mocking

Get introduced to mocking in tests for complex data-driven applications.

What is mocking?

In testing, mocking is the practice of stubbing out something that our code depends on. In a low-level sense, we can think of all the arguments we’ve created to pass into our functions being tested as mock arguments. They are unrelated to the real-world source the function receives arguments from, but instead exist solely for testing purposes.

What types of things do we mock?

As a general rule, we want to mock any external dependencies in our code, like third-party packages, as well as data received or passed into our code. In some instances, we may want to mock local modules that our code depends on as well, such as a class or an API call. Additionally, mocking specific functions can be extremely helpful. This is particularly true in testing client-side code. Often, when assembling components, a function such as a change handler is passed into the component. Since the function is defined outside the component, it isn’t the behavior of the function that we want to test. Instead, we are focused on whether or not the function is called on changes. What the function does is irrelevant here because this is tested in the test suite for the parent component.

Get hands-on with 1200+ tech skills courses.