简体   繁体   English

测试使用browserify别名和填充的CommonJS模块

[英]Testing CommonJS modules that use browserify aliases and shims

Browserify allows creating aliases and shimming modules that are not directly CommonJS compatible. Browserify允许创建不直接与CommonJS兼容的别名和填充模块。 Since I'd like to run my tests in node CLI, can I somehow handle those aliases and shimmed modules in node? 由于我想在节点CLI中运行测试,因此我可以以某种方式处理节点中的那些别名和填充的模块吗?

For example, let's say I'm aliasing ./my-super-module to supermodule and shimming and aliasing some jquery plugin ./vendor/jquery.plugin.js -> ./shims/jquery.plugin.shim.js to jquery.plugin . 例如,假设我走样./my-super-modulesupermodule和匀场和混叠一些jQuery插件./vendor/jquery.plugin.js - > ./shims/jquery.plugin.shim.jsjquery.plugin

As a result, I can do this in my module: 结果,我可以在我的模块中执行此操作:

var supermodule = require('supermodule');
require('jquery.plugin');

// do something useful...
module.exports = function(input) {
  supermodule.process(output)
}

Are there any practices how I could test this module in node.js/cli so that the dependencies are resolved? 有什么实践可以在node.js / cli中测试此模块以解决依赖关系吗?

You might want to use proxyquire if you plan to test this module directly in node using any cli runner. 如果您打算使用任何CLI运行程序直接在节点中测试此模块,则可能要使用proxyquire

using mocha will be something like this 使用摩卡咖啡将是这样的

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')
     });
  });
});

If you want to test this is a real browser, you might not need to mock your aliases modules, you can use browserify to run your tests in karma directly. 如果要测试这是一个真实的浏览器,则可能不需要模拟别名模块,则可以使用browserify直接在karma运行测试。

If you need to mock modules in that scenario you can use proxyquireify , which will allow you to do the same but with browserify . 如果您需要模拟在这情况下,你可以使用模块proxyquireify ,这将允许你做相同,但与browserify

there is also browsyquire which is a fork of proxyquireify that I made with some extra features and a bug fix . 还有一个browsyquire ,它是我用一些额外的功能和错误修复制作的proxyquireify的分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM