var mod = require('module');

module.exports = function (path, mocks) {
  // Reference to the original require function
  var require = mod.prototype.require;

  // Overwrite the require function
  mod.prototype.require = function (path) {
    // Return a mock object, if present. Otherwise, call the original require
    // function in the current file's module context
    return mocks[path] || require.call(this, path);
  };

  // Require the module. Our overwritten require function will be used for its
  // module dependencies
  var file = require(path);

  // Restore the original require function
  mod.prototype.require = require;

  // Return the module
  return file;
};

var mod = require('module');

module.exports = function (path, mocks) {
  // Reference to the original require function
  var require = mod.prototype.require;

  // Overwrite the require function
  mod.prototype.require = function (path) {
    // Return a mock object, if present. Otherwise, call the original require
    // function in the current file's module context
    return mocks[path] || require.call(this, path);
  };

  // Require the module in the parent module context. Our overwritten require
  // function will be used for its module dependencies
  var file = require.call(module.parent, path);

  // Restore the original require function
  mod.prototype.require = require;

  // Return the module
  return file;
};

Fix module require context issue.
Fix module require context issue.

Require the main module (whose dependencies will be mocked) in the
parent context of the mockit module. This fixes an issue where the main
module was being required in the global scope, causing there to be
issues resolving the path correctly.
