测试使用browserify别名和填充的CommonJS模块 [英] Testing CommonJS modules that use browserify aliases and shims

查看:78
本文介绍了测试使用browserify别名和填充的CommonJS模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Browserify允许创建不直接与CommonJS兼容的别名和填充模块。由于我想在节点CLI中运行测试,因此我可以以某种方式处理节点中的那些别名和填充模块吗?

Browserify allows creating aliases and shimming modules that are not directly CommonJS compatible. Since I'd like to run my tests in node CLI, can I somehow handle those aliases and shimmed modules in node?

例如,假设我为 ./ my-super-module supermodule 并给某些jquery插件填充和别名。 /jquery.plugin.js -> ./ shims / jquery.plugin.shim.js jquery.plugin

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.

因此,我可以在我的模块中执行此操作:

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

有没有实践可以在node.js / cli中测试此模块以解决依赖关系?

Are there any practices how I could test this module in node.js/cli so that the dependencies are resolved?

推荐答案

您可能要使用 proxyquire ,如果您打算使用任何cli运行器直接在节点中测试该模块。

You might want to use proxyquire if you plan to test this module directly in node using any cli runner.

使用摩卡(mocha)就是这样

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

如果要测试这是一个真正的浏览器,则可能不需要模拟别名模块,您可以使用 browserify 直接在 karma 中运行测试。

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.

如果您需要在这种情况下模拟模块,则可以使用 proxyquireify ,这将使您能够相同,但使用 browserify

If you need to mock modules in that scenario you can use proxyquireify, which will allow you to do the same but with browserify.

也有 browsyquire ,它是我制作的 proxyquireify 的一个分支,具有一些额外的功能和错误修复

there is also browsyquire which is a fork of proxyquireify that I made with some extra features and a bug fix.

这篇关于测试使用browserify别名和填充的CommonJS模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆