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
var os = require('os');

module.exports = function() {
  return os.freemem();
}

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
// Get some assertion helper.
var assert = require('assert');

// First up we require proxyquire
var proxyquire = require('proxyquire');

// We make a mock of what we need.
var osStub = {
  freemem: function() { return 617619456; }
}

// Now some magic ;)
var freemem = proxyquire("./baz",{'os':osStub});

it("should return the amount of free memory", function(){
    assert.equal(freemem(),6176197456);
});

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!

/images/proxyquire

Kudos to Thorsten Lorenz for the wonderful module.

Suggest a module

Comments