Using Karma, how do I exclude all files that match a pattern except for those within a specific sub-folder?

Kirk Larkin picture Kirk Larkin · Feb 21, 2015 · Viewed 24.9k times · Source

I have a simple AngularJs application setup, that looks a bit like this:

client
    vendor
        angular
        bootstrap
        jquery
        ...
    app.module.js
    app.controller.js
    ...
node_modules
    angular-mocks
    ...

I'm setting up a karma.conf.js file and I want to include angular\angular.js from vendor. I don't want to include anything else from vendor.

Here is what I have in the relevant karma.conf.js sections:

files: [
    'client/vendor/angular/angular.js',
    'client/**/*.js',
    'client/**/*.spec.js'
],
exclude: [
    'client/vendor/**/*.js'
],

The problem I have is simple: I'm including angular.js explicitly in files, but it is then being excluded in the exclude section pattern.

How can I exclude all of client/vendor except for angular/angular.js (and perhaps others later)? The client directory contains a lot of files, sub-folders, etc, that include my own .js files, so it's not easy to just move everything I want to include into a folder of its own, for example.

Answer

Mosho picture Mosho · Feb 21, 2015

Try this:

client/vendor/**/!(angular).js

Example

More filenames can be excluded like this:

client/vendor/**/!(angular|angular-route).js

The patterns used here are called globs.

Here is a short guide to glob functionality in node from node-globs on GH.