dustin.schultz dustin.schultz - 1 year ago 73
AngularJS Question

karma.conf.js automatic file ordering?

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.

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

// Modules: load module first, then all controllers, services, etc

// Load overall app module last

// Load mocks and tests

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.

Answer Source

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

Solution n.1

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.

Solution n.2

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
  window.MyNamespace = window.MyNamespace || {};
  // append things to the namespace...

// module B
  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.

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

Solution n.3

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

Note: this is the less mantainable of the options.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download