karma.conf.js 自动文件排序? [英] karma.conf.js automatic file ordering?

查看:14
本文介绍了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.

我尝试指定一个简单的 glob 模式,例如 **/*.js,但由于运行时它们包含在 Karma 中的顺序,我的许多模块无法加载.据我了解,它是按字母顺序排列的,第一个匹配项.

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?

注意:我考虑的一种可能的解决方案是将所有文件连接在一起,但我用谷歌搜索了其他人是否这样做,但没有找到示例.

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.

使用 AMD/RequireJS:不要一次加载所有模块,只需要它们当你需要的时候.有时它可能无法满足您的需求,使项目变得过于复杂,所以请看下面.

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.

注意:恕我直言,这是最优雅的解决方案和业力 确实支持requireJS.

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

创建一个命名空间,您可以在其中附加所有内容并在 DOM 准备好时启动它们(通常很快,但这实际上取决于您加载了多少脚本)

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.

以编程方式为您的 Karma 文件排序:我写了关于它在此答案中.

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

注意:这是最不易维护的选项.

Note: this is the less mantainable of the options.

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

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