Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): allow configurable launchers, preprocessors, reporters #533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,37 @@ var KarmaDsl = function(config) {
config[key] = newConfig[key];
});
};

// this.defineLauncher
// this.defineReporter
// this.definePreprocessor
['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]
};

if (!helper.isString(name)) {
return log.warn('Can not define %s. Name has to be a string.', type);
}

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

if (!helper.isObject(options)) {
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);
};
}, this);
};

var parseConfig = function(configFilePath, cliOptions) {
Expand Down
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ exports.start = function(cliOptions, done) {
customFileHandlers: ['value', []],
customScriptTypes: ['value', []],
reporter: ['factory', reporter.createReporters],
capturedBrowsers: ['type', browser.Collection]
capturedBrowsers: ['type', browser.Collection],
args: ['value', {}]
}];

// load the plugins
Expand Down
50 changes: 50 additions & 0 deletions test/unit/config.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,53 @@ describe 'config', ->
expect(pattern.included).to.equal true
expect(pattern.watched).to.equal false
expect(pattern.served).to.equal false


describe 'DSL', ->
di = require 'di'
dsl = config = null

forwardArgsFactory = (args) ->
args

baseModule =
'preprocessor:base': ['type', forwardArgsFactory]
'launcher:base': ['type', forwardArgsFactory]
'reporter:base': ['type', forwardArgsFactory]

beforeEach ->
config = {plugins: []}
dsl = new m.KarmaDsl config


it 'should define a custom launcher', ->
dsl.defineLauncher 'custom', 'base', {first: 123, whatever: 'aaa'}

injector = new di.Injector([baseModule].concat config.plugins)
injectedArgs = injector.get 'launcher:custom'

expect(injectedArgs).to.be.defined
expect(injectedArgs.first).to.equal 123
expect(injectedArgs.whatever).to.equal 'aaa'


it 'should define a custom preprocessor', ->
dsl.definePreprocessor 'custom', 'base', {second: 123, whatever: 'bbb'}

injector = new di.Injector([baseModule].concat config.plugins)
injectedArgs = injector.get 'preprocessor:custom'

expect(injectedArgs).to.be.defined
expect(injectedArgs.second).to.equal 123
expect(injectedArgs.whatever).to.equal 'bbb'


it 'should define a custom reporter', ->
dsl.defineReporter 'custom', 'base', {third: 123, whatever: 'ccc'}

injector = new di.Injector([baseModule].concat config.plugins)
injectedArgs = injector.get 'reporter:custom'

expect(injectedArgs).to.be.defined
expect(injectedArgs.third).to.equal 123
expect(injectedArgs.whatever).to.equal 'ccc'