Skip to content

Commit

Permalink
remove init (simplify api by inferring basedir from module.parent)
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Feb 5, 2013
1 parent 0e83e22 commit 2274ca4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 44 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ Use moquire to test your modules in isolation without trying to hack around `req

## usage

Pass in `__dirname` to initialize `moquire` with the current file's directory (so it can resolve relative paths)

var moquire = require('moquire')(__dirname)
var moquire = require('moquire')

var moduleUnderTest = moquire('../test', {
depA: {},
Expand Down
66 changes: 32 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
var vm = require('vm')
var fs = require('fs')
var path = require('path')
var resolve = require('resolve')

function load(path, cache) {
var cache = {}
var basedir = path.dirname(module.parent.filename)

function load(path) {
return cache[path]
|| (cache[path] = fs.readFileSync(path, 'utf8'))
}

var init = function (basedir) {
var files = {};

return function moquire(path, mocks) {
var resolved = resolve.sync(path, {basedir: basedir})

mocks = mocks || {};
var exports = {};
var context = {
require: function (path) {
return mocks[path] || require(path)
},
exports: exports,
module: {
exports: exports
}
function moquire(path, mocks) {
var resolved = resolve.sync(path, {basedir: basedir})

mocks = mocks || {};
var exports = {};
var context = {
require: function (path) {
return mocks[path] || require(path)
},
exports: exports,
module: {
exports: exports
}
}

var source = load(resolved, files)

vm.createScript(source, resolved)
.runInNewContext(context)

var exported;
if (context.module.exports === context.exports) {
exported = context.module.exports;
} else if (context.exports === exports) {
exported = context.module.exports;
} else {
exported = context.exports;
}
var source = load(resolved)

return exported;
}
}
vm.createScript(source, resolved)
.runInNewContext(context)

var exported;
if (context.module.exports === context.exports) {
exported = context.module.exports;
} else if (context.exports === exports) {
exported = context.module.exports;
} else {
exported = context.exports;
}

return exported;

}

module.exports = init;
module.exports = moquire;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moquire",
"version": "1.0.0",
"version": "1.1.0",
"description": "because mocking `require`d modules for unit tests should be easy",
"main": "index.js",
"directories": {
Expand Down
7 changes: 1 addition & 6 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
var moquire = require('../index')(__dirname)
var moquire = require('../index')
var chai = require('chai')
chai.should()

describe('moquire', function () {

it('is initialized by passing in __dirname from the calling module', function () {
var moquire = require('../index')
moquire('base').should.be.a('function')
})

it('uses the same path identifiers as node require', function () {
var a = require('./a')
var a2 = moquire('./a')
Expand Down

2 comments on commit 2274ca4

@thlorenz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for module.parent to work, you need to delete 'index' from the module cache after it has been required. (see https://github.com/thlorenz/proxyquire/blob/master/index.js#L7)

Otherwise the parent will always be the first test module that required it and relative resolve will not work if other test modules are in different paths.

@junosuarez
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks for the tip!

Please sign in to comment.