This video is available to students only

Introduction to Test Doubles

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Previous LessonSet up Jest extensionNext LessonMock database

Lesson Transcript

  • [00:00 - 01:12] In this lesson, we'll explore the concepts of test doubles, which includes spies, stobs and mocks. These powerful tools allow developers to focus on testing their code without being burdened by the complexities of dependencies or collaborators. To better understand test doubles, let's draw an analogy with the film industry. Just as actors have stunned doubles to handle dangerous scenes, developers have test doubles to handle complex dependencies or collaborators. Test doubles provide a simplified version of components, allowing us to focus solely on testing our code. In this image, the illustration on the left is that of a system on the test that executes with all of its dependencies, while the illustration on the right is that of a system on the test that executes in an isolated manner. All the dependencies have been replaced with test doubles. Let's start with spies. In just, spies can be created using the built- in jest.spyOn function.

  • [01:13 - 01:56] Spies allows us to observe the behavior of a function or method during its execution. In this example, we have a test case that verifies whether the spaswad correct function calls the bcrypt.compare function on each execution. By spying on bcrypt.compare and asserting on its calls, we can ensure the proper interaction between the spaswad correct function and bcrypt.compare. To clear the recorded calls of a spy, we can use the more clear method. This allows us to start afresh with each test case and accurately track the number of calls made to the spied function.

  • [01:57 - 02:55] Next, let's discuss stops. Tops are test doubles that replace the function or method with a simplified implementation. They allow us to provide predefined responses without executing the original code. In jest, we can quickly create stops using jest.fn function. This creates a stop that does nothing by default returning undefined. To customize the behavior of a stop, we can use jest.spyOn.smokeimplementation to provide a custom implementation or we can use jest.spyOn.smoke resolved value to return a resolved promise. This allows us to control the stop 's response to suit our testing needs, as we've done in both lines here. To clear the calls and restore a stop to its initial state, we can use mock reset. This allows us to start afresh with each test case.

  • [02:56 - 04:07] If we want to completely remove the stop, restoring the original implementation , we can use mock restore. Instead of literally in our test cases with spy.mockreset or mock restore or mock clear, we can equally achieve the same using jest.config.js file. The claimless configuration in jest.config.js file is equivalent to the mock clear function here. While the reset mock configuration option here is equivalent to the spy.mock reset. Lastly, the restore mock configuration option is equivalent to the spy.mock restore. Having set this to true, it means that jest will automatically reset and restore our mock after each test. Lastly, let's discuss mock. Mocks are test doubles that replace entire objects or modules. They allow us to simulate the behavior of dependencies and control their responses to suit our testing needs. We'll use the jest.mock function in future lessons when we mock our HTTP services.

  • [04:08 - 04:35] In summary, test doubles such as spies, stubs, and mocks are invaluable tools in unit testing. They simplify the testing process by allowing developers to focus on their code without worrying about the complexities of external components. By utilizing jest's spy , stub, and mock functionalities, we can create robust and reliable unit tests.

This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Pain Free Mocking with Jest