From the course: Node.js: Testing and Code Quality
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Mock an entire module with Jest
From the course: Node.js: Testing and Code Quality
Mock an entire module with Jest
- [Presenter] So far, we've mocked a single function. Let's mock an entire module with Jest, but how does it work? in no Jest and in most languages, units of code usually have dependencies. That's a good thing, because it promotes re-usability and modularity. As an example, our application has dependencies on the sequel light database and the debugger module for logging. Of course, you can write tests that use other components, but testing dependencies isn't unit testing, it's integration testing. So, how do we isolate a unit with the dependency? The solution is to manipulate no Jests require cash by substituting your own code in place of the target dependency. The nice thing about this technique, is that it needs no code changes to the target require to work. Like many other forms of monkey patching and JavaScript Manipulation, changes to the require cash can be done manually. With that said, doing it yourself head…
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)
-
-