require a module with mocked dependencies

npm install mockuire
6 downloads in the last week
101 downloads in the last month

This is not a mocking library. This module allows you to require a module and pass mocks for its dependencies.

Dependencies that are not passed will be solved normally.

This module uses vm.runInNewContext and is heavily inspired by this answer in stackoverflow answer.


npm install mockuire


Given a file like this one foo.js:

var path = require("path");

module.exports = function(a, b){
    return path.join(a, b, "burbujas");

then we can test as follows:

  var mockuire = require("mockuire")(module);

  exports.test = function (test) {
    var mockedPath = { 
      join: function() { 
          return, 0).join("!"); 
      foo = mockuire("./fixture/foo", { path: mockedPath }),
      result = foo( "a", "b" );

    test.equal( result, "a!b!burbujas" );

You have to pass the module in order to fallback to the module require when needed.

Optional compilers

If your SUT is coffee script use this syntax:

var mockuire = require("mockuire")(module, { "coffee": require("coffee-script") });

where "coffee" is the extension and the next thing needs to have a compile function.

Contrib - run tests

npm test


MIT License

npm loves you