-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove init (simplify api by inferring basedir from module.parent)
- Loading branch information
1 parent
0e83e22
commit 2274ca4
Showing
4 changed files
with
35 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2274ca4
There was a problem hiding this comment.
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.
2274ca4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thanks for the tip!