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

雨燕双飞 提交于 2019-12-03 04:22:59

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']
  }
}

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
TDDdev

You can use require statements in karma config files.

for example, in your karma config file you could do:

files: require('./karma.conf.files')

For a fuller answer .. I found another solution from this link: https://groups.google.com/forum/#!topic/karma-users/uAf7TuVBmCQ

With karma 0.8 (current stable)

// shared_conf.js
module.exports = {
  port: 8080
};

// karma1.conf.js
var shared = require('./shared_conf.js');

port = shared.port;

With karma 0.9 (currently in canary release):

// shared_conf.js
module.exports = function(karma) {
  karma.configure({
    port: 8080  
  });
};

// karma1.conf.js
var shared = require('./shared_conf.js');
module.exports = function(karma) {
  shared(karma);
  karma.configure({
    // override
  });
};

This worked for me to pull in an array of file names from a separate config file.

I noticed that whatever argument you pass into command line you get this argument camelCased in initial config object.

> karma start i-wanna-debug

module.exports = function (config) {
    config.set({
        singleRun: config.iWannaDebug ? false : true
    });
};

This allows you to use single Karma config file with multiple configurations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!