Big JS app testing - avoiding multiple karma.conf.js files

gruntjs, javascript, karma-runner, testing

Solution

grunt-karma sounds ideal for your needs.

It is a grunt multitask which allows sub tasks to have a common configuration, which can be overridden by specific sub tasks.

The plugin consolidates Karma configuration into a single file. For example:

karma: {
  options: {
    configFile: 'karma.conf.js',
    runnerPort: 9999,
    browsers: ['Chrome', 'Firefox']
  },
  category1: {
    files: ['app/js/category1/**/*.js', 'test/category1/*.js']
  },
  category2: {
    files: ['app/js/category2/**/*.js', 'test/category2/*.js']
  }
}

Problem

I use karma + jasmine + phantom for my headless Javascript tests. The problem I have is that I have a really big app consisting of a lot of JS modules which I want to test. So I need custom mocks for each case and custom includes for each case. `karma.conf.js` allows me only to have `files` array which consist of patterns for all the files to include which is GREAT if my app would be small and not a big app with ton of files and modules. My solution for now - create multiple `karma.conf.js` files for each test case. But this really sucks as having so lot of config files is a big bloat and if I would want to change one setting(like autoWatch) I would need to change all the config files. My other solution - write custom handler in front of `karma.conf.js` to handle additional parameters(spec file or folder to bypass karma for searching it's config file) and simply build `files` array dynamically. Now the problem I see with this is that karma runs only once and I would be limited to run one test spec... and I DO NOT WANT TO MODIFY KARMA ITSELF. I have also considered using Grunt but haven't found a way to make it work for multiple test cases. By the way, my ideal structure would be like this: to have files: ``` test/specs/category/unit1_spec.js test/mocks/category/unit1_mock.js ``` config file: ``` files: [ { 'includes': [array_of_includes], 'spec': 'spec_file' } ] ``` mock file would be grabbed automatically from appropriate mocks directory. and I could do `karma start test/specs/category` and it would recursively run all the test cases inside the folder. tl;dr - I want to test comfortably a big app. I would appreciate any suggestion to handle this task.

Original source