Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

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