测试使用browserify别名和shim的CommonJS模块
Testing CommonJS modules that use browserify aliases and shims
Browserify允许创建不直接兼容CommonJS的别名和闪烁模块。因为我想在节点CLI中运行我的测试,我能以某种方式处理节点中的这些别名和模糊模块吗?
例如,假设我将./my-super-module
混叠到supermodule
,并将一些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,以解决依赖关系?
如果您打算使用任何cli运行器直接在node中测试此模块,则可能需要使用proxyquire。
使用mocha会像这样
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')
});
});
});
如果您想测试这是一个真实的浏览器,您可能不需要模拟您的别名模块,您可以使用browserify
直接在karma
中运行您的测试。
如果您需要在这种情况下模拟模块,您可以使用proxyquireify,它将允许您执行相同的操作,但使用browserify
。
还有一个browsyquire,它是proxyquireify
的一个分支,我做了一些额外的功能和一个错误修复。
相关文章:
- 如何捆绑commonjs模块,除非指定需要
- AMD应用程序在浏览器中的加载速度是否快于CommonJS应用程序
- Gulp,浏览使用react作为CommonJS不起作用.React未定义
- 对CommonJS配置文件使用全局变量
- 如何在JSFiddle上使用CommonJS模块
- RequireJs shim:如何防止 define() {..} 中重复的模块名称
- 如何使用默认函数导出为 commonjs 模块创建类型
- RequireJS不使用commonjs需要语法
- 定义CommonJS模块时,fn.call(this)与fn()的对比
- Backbone.js应用程序使用CommonJS结构与node.js和客户端协同工作
- 可以Typescript导入CommonJS模块
- Typescript:从CommonJS模块导入默认值,从打字文件导出附加类型
- CommonJs + titanium
- 类commonjs系统中的绝对引用
- 打包具有依赖关系的Browser/Server CommonJS模块
- 为AMD、CommonJS等打包js库
- 调用module.exports上的函数时发生CommonJS非法调用错误
- Knockoutjs if/shim binding
- Require.js+R.js优化器忽略Shim
- 测试使用browserify别名和shim的CommonJS模块