Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including moquire with all npm packages installed. Try it out:

var moquire = require("moquire")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

moquire v1.5.5

because mocking `require`d modules for unit tests should be easy


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


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free