简体   繁体   English

Karma和RequireJS:避免在src中重复并测试RequireJS配置

[英]Karma and RequireJS: avoid duplication in src and test RequireJS config

For example, this test-main.js and this main.js has duplicated paths and shims. 例如,这个test-main.js和这个main.js有重复的路径和填充程序 For large projects, there may be lots of them. 对于大型项目,可能有很多项目。 I can even use grunt-bower-requirejs plugin to add installed Bower components into main.js, but after that, I have to copy them to test-main.js, either by hand or by program. 我甚可以使用grunt-bower-requirejs插件将已安装的Bower组件添加到main.js中,但之后,我必须手动或通过程序将它们复制到test-main.js。

Is there any convenient way to avoid this duplication, for example, tell RequireJS to include another config file? 有没有方便的方法来避免这种重复,例如,告诉RequireJS包含另一个配置文件?

I also hate to duplicate information. 我也讨厌复制信息。 I work around that problem by using in my test suite an HTML setup which calls require.config twice. 我通过在我的测试套件中使用一个调用require.config两次的HTML设置来解决这个问题。

So HTML in my test suite first loads RequireJS, and then the general configuration, which is in a file named requirejs-config.js : 因此,我的测试套件中的HTML首先加载RequireJS,然后是一般配置,它位于名为requirejs-config.js的文件中:

<script type="text/javascript" src="lib/requirejs/require.js"></script>
<script type="text/javascript" src="requirejs-config.js"></script>

The requirejs-config.js file is nothing special. requirejs-config.js文件没什么特别的。 Here's an abbreviated version: 这是一个缩写版本:

require.config({
 baseUrl: 'lib/',
 paths: {
   browser_test: '../../../browser_test',
   jquery: 'external/jquery-1.10.2',
   bootstrap: 'external/bootstrap/js/bootstrap.min',
   // [...]
 },
 packages: [
     {
         name: "lodash",
         location: "external/lodash"
     }
 ],
 shim: {
   bootstrap: {
     deps: ["jquery"],
     exports: "jQuery.fn.popover",
   },
   // [...]
 },
 config: {
   // [...]
 },
 enforceDefine: true
});

Then there is a <script> element which calls require.config with the settings that apply to the testing environment: 然后有一个<script>元素,它调用require.config以及适用于测试环境的设置:

<script>
  require.config({
      paths: {
          'mocha': '/node_modules/mocha',
          'chai': '/node_modules/chai/chai'
      },
      shim: {
          'mocha/mocha': {
              exports: "mocha",
              init: function () { this.mocha.setup('bdd'); return this.mocha; }
          }
      },
      config: {
        // [...]
      }
  });
</script>

(The 'mocha/mocha' shim looks funny but it is correct.) 'mocha/mocha'垫片看起来很有趣,但它是正确的。)

In this specific case, the second call to require.config only adds new values to paths , shim and config but it is also possible to override earlier values. 在这种特定情况下,第二次调用require.config只会向pathsshimconfig添加新值,但也可以覆盖之前的值。 I could for instance change the path to which 'jquery' resolves or change Bootstrap's shim. 我可以更改'jquery'解析的路径或更改Bootstrap的垫片。

You can see that I'm using Mocha + Chai for my test suite but the solution above is really not specific to Mocha at all. 你可以看到我在我的测试套件中使用Mocha + Chai,但上面的解决方案根本不是Mocha特有的。

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

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