Kevin Renskers
Kevin Renskers

Reputation: 5892

How to smartly include all the necessary files?

Right now my Karma config has the following files to include:

    files: [
        'vendor/vendor.js',
        'vendor/angular-mocks/angular-mocks.js',
        'src/components/security/index.js',
        'src/components/security/authentication.js',
        'src/components/security/login-controller.js',
        'src/components/filters/index.js',
        'src/components/filters/toDate.js',
        'src/components/services/index.js',
        'src/components/services/alert.js',
        'src/menu/index.js',
        'src/menu/menu-controller.js',
        'src/user/index.js',
        'src/manage/index.js',
        'src/manage/user/manage-user-controller.js',
        'src/manage/channel/manage-channel-controller.js',
        'src/stream/index.js',
        'src/stream/stream-controller.js',
        'src/messages/index.js',
        'src/messages/messages-controller.js',
        'src/app.js',
        'src/**/*.spec.js'
    ],

The file vendor/vendor.js is automatically created by a Gulp task concatenating all vendor files using my bower config. It's all my own Javascript code that's so hard to include because order matters a great deal. The index.js files within a folder define the module (and its routes) and thus must be loaded before the individual files. And app.js always has to be last.

So my question is how I do this a bit smarter, for example with a glob that first includes all the index.js files and then all the others?

Upvotes: 4

Views: 229

Answers (2)

Kevin Renskers
Kevin Renskers

Reputation: 5892

Hm I guess asking the question gave me the idea I needed :) This works:

    files: [
        'vendor/vendor.js',
        'vendor/angular-mocks/angular-mocks.js',
        'src/*/**/index.js',
        'src/*/**/*.js',
        'src/app.js',
        'src/**/*.spec.js'
    ],

Adding the *.js after index.js results in debug messages like this:

src/manage/index.js ignored. Already in the list.

Excellent!

Upvotes: 2

David M. Karr
David M. Karr

Reputation: 15205

This is exactly what RequireJS is for. You can use it in your deployed code, but you can also just use it for your tests.

If you do this, your "karma.conf.js" ends up being a little shorter and less volatile. Your RequireJS config file specifies some dependency mapping. Each test spec ends up specifying what dependencies it needs, either in a "declarative" fashion in the "define" call, or sometimes manually through the "require" function (you sometimes need the latter to deal with circular reference problems).

Upvotes: 2

Related Questions