Saturday, 24 November, 2018 UTC


Summary

Sometimes when writing a unit test, you know that the module you're testing imports a module that you would like to observe, or at the very least mock to prevent side effects like network activity or file system operations. For JavaScript unit tests that run in Node, we can hijack the built-in `require` function by using the [`proxyquire`](https://www.npmjs.com/package/proxyquire) module, which allows us to mock one or more modules that are second-class dependencies in our unit test, replacing them with whatever we want. This means we could replace functions with [no-ops](https://en.wikipedia.org/wiki/NOP_%28code%29) to prevent side effects, or replace them with [Sinon spies](https://egghead.io/lessons/mocha-unit-test-a-function-that-invokes-a-callback-with-a-sinon-spy) to observe the inner workings of the module you're testing. In this video, we'll demonstrate the basics of using Proxyquire to prevent and observe a file system operation.