0

I am working on a code base, where code is divided into multiple require modules. ie each section has its own main.js with the require configuration.

I want to setup code coverage for the whole code base using Karma.

Since each section has its own requirejs config, I created a test-main.js for each module.

And have karma.config load all the test-main.js files.

I am running into issues. There is collision between baseUrl.

When testing only one module, it works fine.

Any idea ?

Ravi
  • 323
  • 2
  • 6
  • 18
  • What happens if you chain multiple karma runners with different configurations? – MarcoL Mar 17 '15 at 21:27
  • That is a good suggestion. But is it possible to chain multiple karma runners ? I googled it and no luck. – Ravi Mar 18 '15 at 19:44

1 Answers1

0

You can chain multiple karma runners using karma programmatically. Karma offers an API to make it work programmatically (which is not that clear IMHO - some examples can improve it).

First of all you need an array of configurations then you need to call karma programmatically and chain the calls.

Create an array of configurations

function getConfiguration(filename){
  return {
    // you can enrich the template file here if you want
    // e.g. add some preprocessor based on the configuration, etc..
    //
    // if not set in the config template 
    // make Karma server launch the runner as well
    singleRun: true,
    // point to the template
    configFile : __dirname + filename
  };
}

function createConfigurations(){
    return [getConfiguration('conf1.js'), getConfiguration('conf2.js'), etc...];
}

Start Karma programmatically

function startKarma(conf, next){
  karma.server.start(conf, function(exitCode){
    // exit code === 0 is OK
    if (!exitCode) {
      console.log('\tTests ran successfully.\n');
      // rerun with a different configuration
      next();
    } else {
      // just exit with the error code
      next(exitCode);
  }
});

Chain the karma runners

// Use the async library to make things cleaner
var async = require('async');
var karma = require('karma');

var confs = createConfigurations();
async.eachSeries(confs, startKarma, function(err){
  if(err){
    console.log('Something went wrong in a runner');
  else {
    console.log('Yay! All the runners have finished ok');
  }
});
MarcoL
  • 9,829
  • 3
  • 37
  • 50
  • ok. I just tried running this block of code right out of (http://karma-runner.github.io/0.12/dev/public-api.html) and it starts the server, but doesn't launch the browser and run the tests. What am I missing ? var server = require('karma').server; server.start({port: 9876}, function(exitCode) { console.log('Karma has exited with ' + exitCode); process.exit(exitCode); }); – Ravi Mar 18 '15 at 20:34
  • I tried your approach. It calls only the first config in the array and stops. – Ravi Mar 18 '15 at 21:13
  • I've updated the code to make the runner be triggered by karma and the require. What does it print in the console before stopping? – MarcoL Mar 18 '15 at 21:18