9

I am including Mocha.js with the excellent use shim for a Require.js-based site.

How do I access the define() and it() BDD functions declared by Mocha when using Require.js?

Here is a basic code example:

test.js:

var mocha    = require('use!mocha')
  , testFile = require('testFile.js')

mocha.setup('bdd');
mocha.run();

testFile.js:

define(function(require) {
  // describe() and it() are not available
  describe('Book', function() {
    it('should have pages', function() {

    });
  });
});

I get the error Uncaught ReferenceError: describe is not defined when running in the browser.

I have tried window.describe and tried moving the require('testFile.js') to after the mocha.setup('bdd'). I know I am missing something. Probably passing the context to mocha somehow.

2 Answers 2

13

The problem is that the global functions such as describe and it are set up by mocha.setup(). You can use shim config's init property to call mocha.setup() before mocha is exported.

requirejs.config({
  shim: {
    'mocha': {
      init: function () {
        this.mocha.setup('bdd');
        return this.mocha;
      }
    }
  }
});

require(['mocha', 'test/some_test'], function (mocha) {
  mocha.run();
});

Test files need to require mocha.

define(['mocha'], function (mocha) {    
  describe('Something', function () {
    // ...
  });
});

Shim config's init property was introduced in RequireJS 2.1. You might be able to use exports property instead of init with RequireJS 2.0.

3
  • 1
    Alas for Node users: 'Error: Shim config not supported in Node' Jun 13, 2013 at 20:10
  • this worked for me (requirejs+configfile+testem+mocha)
    – oak
    Mar 20, 2014 at 11:57
  • Worked for me too. Thank you very much. Jun 22, 2015 at 17:55
6

I found the solution in geddski's amd-testing examples project.

Instead of including the test file(s) at the top along with mocha like so:

define(['use!mocha', 'testFile'],
function(Mocha, TestFile) {
  mocha.setup('bdd');
  mocha.run();
});

The test file(s) should be included as another require call and mocha.run() embedded in the callback:

define(['use!mocha'],
function(Mocha) {
  mocha.setup('bdd');

  // Include the test files here and call mocha.run() after.
  require(['testFile'],
  function(TestFile) {
    mocha.run();
  });
});
6
  • Also, you'll want to pass the done param in it() and call done() in your test cases. Otherwise your test may throw an error but mocha will claim it passed. Feb 23, 2012 at 22:41
  • Yuck! Is this still the best solution? Sep 14, 2012 at 17:20
  • Ethan - I hope there's a better solution. Please post another answer if you do find one! Sep 24, 2012 at 18:48
  • My solution was to not setup Mocha / Chai as dependencies in my RequireJS build. It ends up being too convoluted, and for what in the end? I still use RequireJS to build all my tests into a single script (and manage test only dependencies), but I assume now that I've loaded app, Mocha, Chai, then tests. You can either pass these as arguments to a runner like Testacular, or use a test runner HTML page with the script tags manually ordered. I feel this is a good compromise solution. Oct 7, 2012 at 23:18
  • Note that I also always findNestedDependencies, and I don't load scripts from CDNs or with the 'async' style requires... so this solution may or may not work for you depending on your particular situation. Oct 7, 2012 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.