Functions

Loading "Intro to Functions"
Functions are the heart and soul of any logic in JavaScript. Event handlers, callbacks, utilitiesโ€”you have likely worked with functions before. Whenever you want your code to do something you reach out to a function because it represents action.
Ideally, testing functions should come down to the I/O (input/output) testing. You provide a function with the right input and assert the expected output. This testing strategy is extremely prevalent in unit testing and it truly shines with pure functions.
But the thing is, functions, much like the intentions behind them, can be different. Not all functions you will write will be pure. Some will have side effects, some will be asynchronous, and others will introduce dependencies or non-deterministic behaviors. There may also be times when the function itself doesn't matter, and instead you would want to test if it's being called or not.
There are plenty of cases where mocking is useful when testing functions, and itโ€™s time you learned how to do it.

Mocking functions

Mocking imbues your functions with superpowers. There are multiple techniques for mocking functions, and they all come down to the following points:
  1. Recording function calls;
  2. Replacing function implementations;
  3. Creating placeholder functions to use as arguments.
In this exercise block, you will learn how to wield all three and explore use cases for different types of function mocks. Let's begin!