Proxyquire
Test cases always involves mocks and stub, sometimes there will be a need to mock require itself, that where proxyquire comes for our rescue.
Proxyquire: Proxies nodejs require in order to allow overriding dependencies during testing.
Installing proxyquire: npm install -D proxyquire
Example:
Say we a file baz.js under test and it looks like:
1 2 3 4 5 | |
It's evident that the return value of this function will never be a constant value.
How do we test it then?
Have a look at the test case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
That's it! The test case will be green now :)
Proxyquire basically, replaces a module's require function.
There are many other features like:
- 'noCallThru',
- 'callThru',
- 'noPreserveCache',
- 'preserveCache',
- 'load'
Please feel free to go through their extensive API DOC
GIF FTW!

Kudos to Thorsten Lorenz for the wonderful module.