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

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…

Contents