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

lukas.pukenis picture lukas.pukenis · Nov 17, 2013 · Viewed 13.8k times · Source

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.

Answer

syrnick picture syrnick · Jun 5, 2014

Use an ENV variable to pass the argument to files in karma.conf.js:

files: [
  include1, include2, ..., includeN,
  process.env.JS_TESTS + "/*.js"
]

Then run karma like so:

JS_TESTS=test/category2 karma start karma.conf.js