From the course: Node.js: Testing and Code Quality
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Mocking module dependencies
From the course: Node.js: Testing and Code Quality
Mocking module dependencies
- [Instructor] Mocking module dependencies can seem daunting but the technique is really similar to what we've already done. As jest.mock manipulates the require cash we can use jest.mock before requiring the target module in order to alter module dependencies. When ready then require the target module. What's a practical use case? Within reservations.js this same function uses debug to log a developer only message. Then it uses connects to insert the reservation into our database, which returns a promise. For the purposes of testing, I only care if debug was called and that insert was called with a reservation. In order to do that we're going to mock both debug and knex prior to requiring reservations.js. Previously, we relied on jest.mock to completely replace the whole module. This time we're going to use the factory argument to override the default mocking. Debug, once initialized with a label is a function that…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Replacing code with test doubles6m 51s
-
(Locked)
Mocking functions for test code7m 1s
-
(Locked)
Writing your first Jest mock5m 46s
-
(Locked)
Mock an entire module with Jest7m 22s
-
(Locked)
Mocking module dependencies6m 19s
-
(Locked)
Spying to observe interactions7m 3s
-
(Locked)
Challenge: Mock a successful reservation creation53s
-
(Locked)
Solution: Mock a successful reservation creation6m 11s
-
(Locked)
-
-