karma-runnerwallaby.js

How to handle wallabyjs and karma configuration (with requirejs)


After reading the answers and comments on the topic Wallaby on a build server (CI) I accepted that wallabyjs is currently not ready for a ci scenario. Ok, but I am still questioning myself how to handle the typical scenario, that one uses wallabyjs on the client and karma (or another test runner) on the ci system. Especially when using requirejs. As it is explained here there is a

test-main.js — which configures require.js for the tests

Using wallabyjs this looks more or less like

// delaying wallaby automatic start
wallaby.delayStart();

requirejs.config({
  baseUrl: '/src',

  paths: {
    'jquery': '../lib/jquery',
    'underscore': '../lib/underscore'
  },

  shim: {
    'underscore': {
      exports: '_'
    }
  }
});

require(wallaby.tests, function () {
  wallaby.start();
});

Using karma as it is explained here, it looks more or less like this

var TEST_REGEXP = /(spec|test)\.js$/i;
var allTestFiles = [];

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
    // then do not normalize the paths
    var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
    allTestFiles.push(normalizedTestModule);
  }
});

require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base/src',

  // example of using a couple path translations (paths), to allow us to refer to different library dependencies, without using relative paths
  paths: {
    'jquery': '../lib/jquery',
    'underscore': '../lib/underscore',
  },

  // example of using a shim, to load non AMD libraries (such as underscore)
  shim: {
    'underscore': {
      exports: '_'
    }
  },

  // dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

Do I have to maintain two files? Is there a need for some kind of conditional build? Has anyone experience with this scenario?

Thanks a lot.


Solution

  • You may merge these two files into one to reuse the common part and add some logic to execute certain bits based on the current runner.

    window.wallaby && wallaby.delayStart();
    ...
    if (window.__karma__) {
      ...
    }
    ...
    require.config({
      baseUrl: window.__karma__ ? '/base/src' : '/src',
      ...