node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



because mocking required modules for unit tests should be easy

moquire loads modules in a new vmContext so you can be sure they're tested in isolation.

It uses resolve to support the same module path resolution strategy as node's require.

Use moquire to test your modules in isolation without trying to hack around require.

moquire makes it stupid simple to intercept calls to node's favorite service locator, require.

moquire is designed to be as similar to require as possible - down to the name. The api footprint is intentionally small. If you need more functionality, see sandboxed-module or proxyquire.


$ npm install moquire


var moquire = require('moquire')

var moduleUnderTest = moquire('../test', {
  depA: {},
  depB: function (){}

// moduleUnderTest is loaded with `depA` and `depB` mocked

Some test runners will keep the same instance of moquire between runs when in watch mode (for example, for Test-Driven Development). In this case, you probably don't want caching. You can access a version of moquire with caching disabled like so:

var moquire = require('moquire').nocache


var moquire = require('moquire')
moquire.nocache('foo', {})


Since 1.5.0, moquire supports mocking relquire dependencies like so:

Let's imagine a project laid out like:

├── a.js
├── foo.js
└── test
    └── test.js


var relquire = require('relquire')
var foo = relquire('~/foo')


var moquire = require('moquire')
var fakeFoo = {}
var a = moquire('../a', {'~/foo': fakeFoo})

Of course, you can use relative requires in moquire, too:

var a = moquire('~/a', {'~/foo': fakeFoo})

running the tests

$ npm test

inspiration & kudos


jden @leJDen


(c) 2013 Agile Diagnosis See