Skip to content

Commit

Permalink
feat(config): pass the config object rather than a wrapper
Browse files Browse the repository at this point in the history
**The reasons for this change:**
1/ easier to override config properties
The config object was not exposed and karma.configure() only does shallow merge.
Now, this is possible, eg. config.sauceLabs.testName = 'overridden';
This is helpful especially when using multiple config files (eg. a shared config between multiple projects or having a per developer config file that overrides project defaults).

2/ declarative definition of custom launchers/preprocessors/reporters makes more consistent and therefore easier to explain.

3/ declarative definition of custom launchers/preprocessors/reporters also makes it possible to use these with grunt-karma or from JSON config file.

The change is done in a backwards compatible manner, if one use old APIs such as karma.configure()
or karma.defineLauncher(), it works. Only warning is displayed. These extra APIs will be removed in
v0.10.0.

BREAKING CHANGE: please update your karma.conf.js as follows:
// before:
module.exports = function(karma) {
  karma.configure({port: 123});
  karma.defineLauncher('x', 'Chrome', {
    flags: ['--disable-web-security']
  });
  karma.definePreprocessor('y', 'coffee', {
    bare: false
  });
  karma.defineReporter('z', 'coverage', {
    type: 'html'
  });
};

// after:
module.exports = function(config) {
  config.set({
    port: 123,
    customLaunchers: {
      'x': {
        base: 'Chrome',
        flags: ['--disable-web-security']
      }
    },
    customPreprocessors: {
      'y': {
        base: 'coffee',
        bare: false
      }
    },
    customReporters: {
      'z': {
        base: 'coverage',
        type: 'html'
      }
    }
  });
};
  • Loading branch information
vojtajina committed Jun 28, 2013
1 parent 9b5c496 commit d2a3c85
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 128 deletions.
147 changes: 92 additions & 55 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,40 @@ var normalizeConfig = function(config, configFilePath) {
[preprocessors[pattern]] : preprocessors[pattern];
});


// define custom launchers/preprocessors/reporters - create an inlined plugin
var module = Object.create(null);
['launcher', 'preprocessor', 'reporter'].forEach(function(type) {
var definitions = config['custom' + helper.ucFirst(type) + 's'] || {};

Object.keys(definitions).forEach(function(name) {
var definition = definitions[name];

if (!helper.isObject(definition)) {
return log.warn('Can not define %s %s. Definition has to be an object.', type, name);
}

if (!helper.isString(definition.base)) {
return log.warn('Can not define %s %s. Missing base %s.', type, name, type);
}

var token = type + ':' + definition.base;
var locals = {
args: ['value', definition]
};

module[type + ':' + name] = ['factory', function(injector) {
return injector.createChild([locals], [token]).get(token);
}];
});
});

config.plugins.push(module);

return config;
};

// TODO(vojta): remove
// TODO(vojta): remove in 0.11
var CONST_ERR = '%s is not supported anymore.\n\tPlease use `frameworks = ["%s"];` instead.';
['JASMINE', 'MOCHA', 'QUNIT'].forEach(function(framework) {
[framework, framework + '_ADAPTER'].forEach(function(name) {
Expand Down Expand Up @@ -161,29 +191,34 @@ var CONST_ERR = '%s is not supported anymore.\n\tPlease use `frameworks = ["%s"]
}});
});

var KarmaDsl = function(config) {
var Config = function() {
var config = this;

this.LOG_DISABLE = constant.LOG_DISABLE;
this.LOG_ERROR = constant.LOG_ERROR;
this.LOG_WARN = constant.LOG_WARN;
this.LOG_INFO = constant.LOG_INFO;
this.LOG_DEBUG = constant.LOG_DEBUG;

this.configure = function(newConfig) {
this.set = function(newConfig) {
Object.keys(newConfig).forEach(function(key) {
config[key] = newConfig[key];
});
};

// this.defineLauncher
// this.defineReporter
// this.definePreprocessor
// TODO(vojta): remove this in 0.10
this.configure = function(newConfig) {
log.warn('config.configure() is deprecated, please use config.set() instead.');
this.set(newConfig);
};

// TODO(vojta): remove this in 0.10
['launcher', 'reporter', 'preprocessor'].forEach(function(type) {
this['define' + helper.ucFirst(type)] = function(name, base, options) {
var module = Object.create(null);
var token = type + ':' + base;
var locals = {
args: ['value', options]
};
var methodName = 'define' + helper.ucFirst(type);
var propertyName = 'custom' + helper.ucFirst(type) + 's';

config[methodName] = function(name, base, options) {
log.warn('config.%s is deprecated, please use "%s" instead.', methodName, propertyName);

if (!helper.isString(name)) {
return log.warn('Can not define %s. Name has to be a string.', type);
Expand All @@ -197,13 +232,48 @@ var KarmaDsl = function(config) {
return log.warn('Can not define %s %s. Arguments has to be an object.', type, name);
}

module[type + ':' + name] = ['factory', function(injector) {
return injector.createChild([locals], [token]).get(token);
}];

config.plugins.push(module);
config[propertyName] = config[propertyName] || {};
config[propertyName][name] = options;
options.base = base;
};
}, this);
});


// DEFAULT CONFIG
this.frameworks = [];
this.port = constant.DEFAULT_PORT;
this.runnerPort = constant.DEFAULT_RUNNER_PORT;
this.hostname = constant.DEFAULT_HOSTNAME;
this.basePath = '';
this.files = [];
this.exclude = [];
this.logLevel = constant.LOG_INFO;
this.colors = true;
this.autoWatch = false;
this.reporters = ['progress'];
this.singleRun = false;
this.browsers = [];
this.captureTimeout = 60000;
this.proxies = {};
this.proxyValidateSSL = true;
this.preprocessors = {'**/*.coffee': 'coffee'};
this.urlRoot = '/';
this.reportSlowerThan = 0;
this.loggers = [constant.CONSOLE_APPENDER];
this.transports = ['websocket', 'flashsocket', 'xhr-polling', 'jsonp-polling'];
this.plugins = ['karma-*'];

// TODO(vojta): remove in 0.10
this.junitReporter = {
outputFile: 'test-results.xml',
suite: ''
};

// TODO(vojta): remove in 0.10
this.coverageReporter = {
type: 'html',
dir: 'coverage'
};
};

var parseConfig = function(configFilePath, cliOptions) {
Expand All @@ -228,50 +298,17 @@ var parseConfig = function(configFilePath, cliOptions) {
configModule = function() {};
}

var config = {
frameworks: [],
port: constant.DEFAULT_PORT,
runnerPort: constant.DEFAULT_RUNNER_PORT,
hostname: constant.DEFAULT_HOSTNAME,
basePath: '',
files: [],
exclude: [],
logLevel: constant.LOG_INFO,
colors: true,
autoWatch: false,
reporters: ['progress'],
singleRun: false,
browsers: [],
captureTimeout: 60000,
proxies: {},
proxyValidateSSL: true,
preprocessors: {'**/*.coffee': 'coffee'},
urlRoot: '/',
reportSlowerThan: 0,
// TODO(vojta): remove
junitReporter: {
outputFile: 'test-results.xml',
suite: ''
},
// TODO(vojta): remove
coverageReporter: {
type: 'html',
dir: 'coverage/'
},
loggers: [ constant.CONSOLE_APPENDER ],
transports: [ 'websocket', 'flashsocket', 'xhr-polling', 'jsonp-polling' ],
plugins: [ 'karma-*' ]
};
var dsl = new KarmaDsl(config);
var config = new Config();

try {
configModule(dsl);
configModule(config);
} catch(e) {
log.error('Error in config file!\n', e);
return process.exit(1);
}

// merge the config from config file and cliOptions (precendense)
dsl.configure(cliOptions);
config.set(cliOptions);

// configure the logger as soon as we can
logger.setup(config.logLevel, config.colors, config.loggers);
Expand Down
6 changes: 3 additions & 3 deletions test/client/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '../..',

Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = function(karma) {
// level of logging
// possible values: karma.LOG_DISABLE || karma.LOG_ERROR || karma.LOG_WARN || karma.LOG_INFO || karma.LOG_DEBUG
// CLI --log-level debug
logLevel: karma.LOG_INFO,
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
// CLI --auto-watch --no-auto-watch
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/angular-scenario/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['ng-scenario'],

files: [
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/basic/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

files: [
Expand Down
7 changes: 3 additions & 4 deletions test/e2e/coffee-config/karma.conf.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (karma) ->
karma.configure
module.exports = (config) ->
config.set

frameworks: ['jasmine']

Expand All @@ -14,9 +14,8 @@ module.exports = (karma) ->

reporters: ['dots']

preprocessors: {
preprocessors:
'**/*.coffee': 'coffee'
}

plugins: [
'karma-jasmine'
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/coffee/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

files: [
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/coverage/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

files: [
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/coverageQunit/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['qunit'],

files: [
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/coverageRequirejs/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['mocha', 'requirejs'],

files: [
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/junit/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

files: [
Expand All @@ -12,7 +12,7 @@ module.exports = function(karma) {

reporters: ['dots', 'junit'],

logLevel: karma.LOG_DEBUG,
logLevel: config.LOG_DEBUG,

junitReporter: {
outputFile: 'test-results.xml'
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/mocha/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['mocha'],

files: [
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/qunit/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['qunit'],

files: [
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/requirejs/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Karma configuration
// Generated on Thu Jul 26 2012 14:35:23 GMT-0700 (PDT)

module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',

Expand Down Expand Up @@ -36,7 +36,7 @@ module.exports = function(karma) {

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: karma.LOG_INFO,
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/syntax-error/karma.conf.ignore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

// files to load
Expand All @@ -9,7 +9,7 @@ module.exports = function(karma) {

autoWatch: true,
autoWatchInterval: 1,
logLevel: karma.LOG_INFO,
logLevel: config.LOG_INFO,
logColors: true,

browsers: ['Chrome'],
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/timeout/karma.conf.ignore.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Karma configuration
// Generated on Sun Sep 30 2012 22:44:01 GMT-0700 (PDT)

module.exports = function(karma) {
karma.configure({
module.exports = function(config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '',
Expand Down Expand Up @@ -34,7 +34,7 @@ module.exports = function(karma) {

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: karma.LOG_INFO,
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
Expand Down
Loading

0 comments on commit d2a3c85

Please sign in to comment.