Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src
test
README.md
package.json

README.md

Proxying Your Dependencies

Mocking dependencies which are arguments to a function is really trivial in JavaScript tests, you just make up any objects you need! Mocking dependencies which are imported with a require() call is a bit trickier, but it's still perfectly doable without making any changes to your source code, through proxies.

Consider the example below.

var User = require('./models/User.js');

function subset (user) {
  return {
    name: user.name,
    email: user.email
  };
}

module.exports = function (id, done) {
  User.findOne({ id: id }, function (err, user) {
    done(err, user ? subset(user) : null);
  });
};

In the tests, I use proxyquire to resolve a fake version of the model which would normally query the database. In the tests it just calls back a function, passing a made-up model object.

node test/mapper

tape-proxy.png