testing使用browserify别名和垫片的CommonJS模块
Browserify允许创build不直接与CommonJS兼容的别名和填充模块。 因为我想在节点CLI中运行我的testing,我可以以某种方式处理这些别名和节点中的模块?
例如,假设我将别名./my-super-module
为supermodule
和shimming,并将jquery插件的一些别名./vendor/jquery.plugin.js
– > ./shims/jquery.plugin.shim.js
为jquery.plugin
。
因此,我可以在我的模块中做到这一点:
var supermodule = require('supermodule'); require('jquery.plugin'); // do something useful... module.exports = function(input) { supermodule.process(output) }
有没有什么做法,我可以在node.js / cli中testing这个模块,以便解决依赖关系?
如果您打算使用任何cli runner直接在节点中testing此模块,则可能需要使用proxyquire 。
使用摩卡将是这样的事情
describe('test', function () { var proxyquire = require('proxyquire').noCallThru(); it('should execute some test', function () { var myModule = proxyquire('./my-module', { // define your mocks to be used inside the modules 'supermodule' : require('./mock-supermodule'), 'jquery.plugin': require('./jquery-plugin-mock.js') }); }); });
如果你想testing这是一个真正的浏览器,你可能不需要模拟你的别名模块,你可以使用browserify
直接在karma
运行你的testing。
如果你需要在这种情况下模拟模块,你可以使用proxyquireify ,这将允许你做相同的,但与browserify
。
还有browsyquire这是一个proxyquireify
叉,我做了一些额外的function和错误修复 。