2

I haven't seen anything on this, but I was wondering if it was possible to include multiple karma-conf.js files in a single Karma run? Basically, I'm thinking of having a configuration with coverage and tslint for CI as well as a configuration with just tests for local development.

Skeeterdrums
  • 460
  • 1
  • 6
  • 13

2 Answers2

11

Here's a simple example of a child file karma.conf.child.js referencing the main file karma.conf.js. The child file imports the main settings, makes changes, and exports them again. You can use this import-change-export pattern with other tools too.

In this example, I change the settings to enable reporting on TeamCity CI server:

// This is karma.conf.child.js

// import settings from default config file
var properties = null;
var originalConfigFn = require('./karma.conf.js');
originalConfigFn({ set: function(arg) { properties = arg; } });

// alter settings here:
properties.colors = false;
properties.singleRun = true;
properties.autoWatch = false;
properties.browsers = ['ChromeHeadless'];
properties.reporters.push('teamcity');
properties.coverageIstanbulReporter.reports.push('teamcity');

// export settings
module.exports = function (config) {
  config.set(properties);
};

To run, call the child config file as per the documentation:

karma start ./src/karma.conf.child.js

I'm using Angular CLI, so I invoke ng test like this:

ng test --karma-config=./src/karma.conf.child.js
andrewpm
  • 534
  • 6
  • 12
  • Definitely an interesting take, I'll take a look at this later today and report back. – Skeeterdrums Sep 04 '18 at 13:12
  • I was looking for a way to override some karma conf on the command line (like enforcing test coverage in a CI pipeline), but with `ng test` this seems like the only sane way to do it. Thanks! – chucknelson Aug 09 '22 at 16:28
0

As far as I know Karma only accepts a single configuration file as input, that can be specified as command line argument or as part of the constructor options of the karma server.

Maybe gulp offers an alternative approach for you. I use a gulp file with several tasks. Each task is able to override the default configuration that is given in karma-conf.js, e.g.

console.log('Importing gulp dependencies ...');

var gulp = require('gulp');
var karma = require('karma');
var KarmaServerConstructor = karma.Server;
var karmaStopper = karma.stopper;
var watch = require('gulp-watch');
var commandLineArguments = require('yargs').argv;
var svn = require('gulp-svn');
var exec = require('child_process').exec;
var fs = require('fs');
var os = require('os');
var open = require('gulp-open');
var collect = require('gulp-collect');

console.log('Imports finished.');

gulp.task('watch', function() {

  return gulp //
    .watch('MyTestProject/App/**/*.spec.js', handleFileChanged)
    .on('error', handleGulpError);

  function handleFileChanged(vinyl) {

    var pathForChangedFile = './' + vinyl.replace(/\\/g, '/');

    var karmaOptions = {
      configFile: __dirname + '/karma.conf.js',
      action: 'start',
      browsers: ['PhantomJS'],
      singleRun: true,
      files: firstKarmaFiles //
        .concat([
          {
            pattern: pathForChangedFile,
            included: false
          }
        ]) //
        .concat(lastKarmaFiles)
    };

    var karmaServer = new KarmaServerConstructor(karmaOptions);
    karmaServer.start();
    }    
});
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • Yeah, I saw a similar post about Gulp being used in conjunction with Karma once before, but I didn't want pull Gulp into the project if it wasn't necessary. – Skeeterdrums Nov 14 '17 at 17:18
  • Then you might want to create a ticket to request that feature at https://github.com/karma-runner or extend karma with a start method that accepts an array of files on your own. – Stefan Nov 14 '17 at 17:50