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

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…

Contents