title: Testing with Karma and RequireJS with files in CommonJS syntax
body: <p>I'm working on an angular application that is written in CommonJS syntax and uses a grunt task with the grunt-contrib-requirejs task to translate the source files to AMD format and compile it into one output file. My goal is to make Karma work with RequireJS and keep my source files and spec files in CommonJS syntax.</p>
  
  <p>I've been able to get a simple test passing in AMD format with the following file structure:</p>
  
  <pre><code>-- karma-test
     |-- spec
     |   `-- exampleSpec.js
     |-- src
     |   `-- example.js
     |-- karma.conf.js
     `-- test-main.js
  </code></pre>
  
  <p>and the following files:</p>
  
  <p><strong>karma.conf.js</strong></p>
  
  <pre><code>// base path, that will be used to resolve files and exclude
  basePath = '';
  
  // list of files / patterns to load in the browser
  files = [
    JASMINE,
    JASMINE_ADAPTER,
    REQUIRE,
    REQUIRE_ADAPTER,
    'test-main.js',
    {pattern: 'src/*.js', included: false},
    {pattern: 'spec/*.js', included: false}
  ];
  
  // list of files to exclude
  exclude = [];
  
  // test results reporter to use
  // possible values: 'dots', 'progress', 'junit'
  reporters = ['progress'];
  
  // web server port
  port = 9876;
  
  // cli runner port
  runnerPort = 9100;
  
  // enable / disable colors in the output (reporters and logs)
  colors = true;
  
  // level of logging
  // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
  logLevel = LOG_DEBUG;
  
  // enable / disable watching file and executing tests whenever any file changes
  autoWatch = true;
  
  // Start these browsers, currently available:
  browsers = ['Chrome'];
  
  // If browser does not capture in given timeout [ms], kill it
  captureTimeout = 60000;
  
  // Continuous Integration mode
  // if true, it capture browsers, run tests and exit
  singleRun = false;
  </code></pre>
  
  <p><strong>example.js</strong></p>
  
  <pre><code>define('example', function() {
      var message = "Hello!";
  
      return {
          message: message
      };
  });
  </code></pre>
  
  <p><strong>exampleSpec.js</strong></p>
  
  <pre><code>define(['example'], function(example) {
      describe("Example", function() {
          it("should have a message equal to 'Hello!'", function() {
              expect(example.message).toBe('Hello!');
          });
      });
  });
  </code></pre>
  
  <p><strong>test-main.js</strong></p>
  
  <pre><code>var tests = Object.keys(window.__karma__.files).filter(function (file) {
        return /Spec\.js$/.test(file);
  });
  
  requirejs.config({
      // Karma serves files from '/base'
      baseUrl: '/base/src',
  
      // Translate CommonJS to AMD
      cjsTranslate: true,
  
      // ask Require.js to load these files (all our tests)
      deps: tests,
  
      // start test run, once Require.js is done
      callback: window.__karma__.start
  });
  </code></pre>
  
  <hr>
  
  <p>However, my goal is to write both the source file and the spec file in CommonJS syntax with the same results, like so:</p>
  
  <p><strong>example.js</strong></p>
  
  <pre><code>var message = "Hello!";
  
  module.exports = {
      message: message
  };
  </code></pre>
  
  <p><strong>exampleSpec.js</strong></p>
  
  <pre><code>var example = require('example');
  
  describe("Example", function() {
      it("should have a message equal to 'Hello!'", function() {
          expect(example.message).toBe('Hello!');
      });
  });
  </code></pre>
  
  <p>But despite having the <code>cjsTranslate</code> flag set to <code>true</code>, I just receive this error:</p>
  
  <pre><code>Uncaught Error: Module name "example" has not been loaded yet for context: _. Use require([])
  http://requirejs.org/docs/errors.html#notloaded
  at http://localhost:9876/adapter/lib/require.js?1371450058000:1746
  </code></pre>
  
  <hr>
  
  <p>Any ideas on how this can be accomplished?</p>
  
  <hr>
  
  <p>Edit: I found this issue for the karma-runner repo: <a href="https://github.com/karma-runner/karma/issues/552" rel="noreferrer">https://github.com/karma-runner/karma/issues/552</a> and there's a few comments that may help with this problem, but I haven't had any luck with them so far.</p>