本文介绍了karma.conf.js自动文件排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有特点命大angularjs项目。我想设置的单元测试,但我遇到麻烦karma.conf.js文件顺序设置。

I have a large angularjs project ordered by features. I'd like to setup unit testing but I'm having trouble getting the karma.conf.js file ordering setup.

我想原因在于它们包含在噶然在订货时指定如** / *。JS,但我的很多模块无法加载一个简单的glob模式。据我了解,这是按字母顺序排列,第一个匹配。

I tried specifying a simple glob pattern like **/*.js but many of my modules failed to load due to the ordering that they're included in Karma when ran. As I understand, it's alphabetical, first match.

我可以通过做这样的事情手动搞清楚排序来解决此问题:

I was able to resolve this by manually figuring out the ordering by doing something like this:

// list of files / patterns to load in the browser
files: [
  // External scripts
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-cookies/angular-cookies.js',
  'bower_components/angular-resource/angular-resource.js',
  'bower_components/angular-route/angular-route.js',

  // NOTE: ORDER IS IMPORTANT
  // Modules: load module first, then all controllers, services, etc
  'scripts/module1/module1.js',
  'scripts/module1/**/*.js',
  'scripts/module2/module2.js',
  'scripts/module2/**/*.js',

  // Load overall app module last
  'scripts/app.js',

  // Load mocks and tests
  'test/mock/**/*.js',
  'test/spec/**/*.js'
],

这似乎是这将是累赘保持随着新模块的加入。是否有办法来自动解决排序?

This seems like it will be cumbersome to maintain over time as new modules are added. Is there a way to automatically resolve the ordering?

请注意:一个可能的解决方案,我想是所有的文件一起Concat的,但我GOOGLE上搜索,看看别人都这样做,并没有发现例子

Note: One possible solution I was thinking was to concat all the files together but I googled to see if others are doing this and found no examples.

推荐答案

我觉得你可以考虑不同的解决方案在这里,这取决于你想如何管理你的项目。

I think you may look into different solutions here, depending on how you want to manage your project.

使用:不要一次全部加载模块,但只需要他们的时候你需要。
有时,它可能不适合您的需求做出艰难复杂的多项目,所以下面的样子。

Use AMD/RequireJS: do not load your modules all at once but just require them when you need.Sometimes it may not fit your needs tough making the project over complicated, so look below.

注意:这是恕我直言,最优雅的解决方案和因果报应的。

Note: this is IMHO the most elegant solution and karma does support requireJS.

创建,你附加你的东西,并开始他们当DOM准备一个命名空间(通常很快pretty,但它实际上取决于你有多少脚本加载)

Create a namespace where you append all your stuff and start them when the DOM is ready (usually pretty quick, but it really depends on how much scripts you load)

// module A
// Something like this should be fine
(function(){
  window.MyNamespace = window.MyNamespace || {};
  // append things to the namespace...
})();

// module B
(function(){
  window.MyNamespace = window.MyNamespace || {};
  // append things to the namespace...
})();

// This is very rough but it should give the idea
window.ondomready = MyNamespace.start;

注意:虽然这可能工作,你必须拨弄了一下与你的项目相应地改变结构

Note: while this may work you have to fiddle a bit with your project to change the structure accordingly.

我会去一个以上的,除非你真的恨requireJS和所​​有模块的东西。

I would go for the one above unless you really hates requireJS and all the modules stuff.

编程命令你噶文件:我写了一篇关于它的。

Programmatically order your Karma files: I wrote about it in this answer here.

注意:这就是期权的mantainable少

Note: this is the less mantainable of the options.

这篇关于karma.conf.js自动文件排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:23
查看更多