Search code examples
javascriptunit-testingrequirejsenvjs

How to use requireJs with envjs


Can someone give me some hints about how to combine bumblebee with requireJs? BumbleBee combines Rhino, JSpec, Envjs and Ant to provide an "out of the box" JavaScript testing toolkit. Its very usefull for us because it makes a simple stand-alone-running possible in terminal on ubuntu-boxes.

Our whole JS-code based on requireJs-Modules, and it seems, that required modules are not loaded if they wehre required out of spec-files from envjs/bumblebee/rhine (what kind of stuff ever may be responsible for that), may be due to the fact of asynchronousity in requireJs.

What i've done:

  1. i set up a BumbleBee-Environment following instructions on the github-site mentioned above
  2. The command "ant examples" works fine and both test cases are running successfully
  3. i added require-jquery.js to the dependencies (replacing jquery.js which was initially setup) (this way follows the Tutorial "How to use jQuery with requireJS" - which runs successfully along our whole web application (and works fine)
  4. i wrote some requireJS-Modules, required them in spec-files, and detected very quickly that they will not be loaded (the spec-files will be loaded, and the require-method will be executed, but not ins content)
  5. then i played around with these setup-hints for combining requireJS with Node (rhino is mentioned here also)

These files could be interesting:

lib/bumblebee.js (which i considered as the entrypoint of the whole test-suite):

load('dependencies/js/env.rhino.1.2.js');
window.location="examples/fixture/fixture.html";

load('dependencies/js/jspec/jspec.js');
load('dependencies/js/require-jquery.js');

var runSpec = function(spec) {
    JSpec.exec(spec);
};


define(function(require) {
    console.log("A1");
    var dep = require('dependency');

    //The value returned from the function is
    //used as the module export visible to Node.
    return function () {};
});

require(['dependency'], function(d){
    console.log("A2");
} );


console.log("A3");

var specs = arguments;


jQuery.each(specs, function(index, spec) {
    runSpec(spec);
});

JSpec
  .run({ reporter: JSpec.reporters.Terminal, fixturePath: 'spec/fixtures' })
  .report();

Only "A3" will be printed to console, require-based Code will never be executed here.

Tthen I put the define/require-parts into the spec-files from the example-files and detected also, that i cant use modules with require because that they will not loaded and are not pressent.

Considering the original spec-file from BambleBee-Examples:

describe('Greeter', function() {

    load('examples/src/greeter.js');

    it('greets people', function() {
      expect(example.greeter().greet()).to(eql, 'Hello!');
    });
});

When I now use requireJS-specific modules, they will also not be loaded:

describe('Greeter', function() {

    load('examples/src/greeter.js');

    require(['dependency'], function(d){
        // do something with D...
        console.log("This will **NEVER** be printed");
    } );

    console.log("This **WILL** be printed");
    it('greets people', function() {
      expect(example.greeter().greet()).to(eql, 'Hello!');
    });
});

The Question:

Is there a way to combine Envjs with requireJS or is EnvJS the wrong testing tool for us? Thanks a lot for any hints!


Solution

  • This is a known issue. You can refer to https://github.com/envjs/env-js/issues/7 to get detailed discussion between envjs and requirejs. My solution is to use Phantomjs to run jasmine specs with CI, and you can find the sample project at https://github.com/xiaocong/xiaocong.github.com/tree/master/examples/coffee-bbb-amd-backbone-rest-contacts