Question

I've been trying to use requirejs and js-test-driver along side, and I can't seen to get it working.

I have a minimal configuration like this at the root :

server: http://localhost:9876

load:
 - src/main/resources/web/resources/vendor/requirejs/require.js

test:
 - src/test/js/*.js

A "src/main/js/greeter.js" file defines a silly module :

define(function(require) {

  myapp = {};

  myapp.Greeter = function() {
  };

  myapp.Greeter.prototype.greet = function(name) {
    return "Hello " + name + "!";
  };

  return myapp;
});

And I'm trying to let require load the greeter module in a "src/test/js/greeterTest.js" :

GreeterTest = TestCase("GreeterTest");

require.configure({ ???? });
require([ "src/main/js/greeter" ], function(myapp) {

  GreeterTest.prototype.testGreet = function() {
    var greeter = new myapp.Greeter();
     assertEquals("Hello World!", greeter.greet("World"));
    };

});

Whenever I run this, I get an error because myapp is not defined when creating the Greeter.

So is there a way I can configure require in this case ? I tried :

  • setting the baseUrl property to something like file:///.... to give the location of the file
  • using the gateway configuration to proxy the requests that requirejs might do (although I have no server running to serve the js files, so again I had to use file:///)

Is there something else I should try ?

Thanks

Was it helpful?

Solution

Turns out it is possible, but poorly documented :

  • js-test-driver has a 'serve' settings that lets the test server responds static files
  • once served, the files are available at localhost:42442/test/xxxx (The 'test' prefix is never mentionned, except in a comment low in the doc page).

So this works :


server: http://localhost:9876

load:
  - src/main/resources/web/resources/vendor/requirejs/require.js

test:
  - src/test/js/*.js

serve:
  - src/main/js/*.js

And requirejs has to be configured like this :


require({
    baseUrl : "/test/src/main/js"
}, [ "greeter" ], function(myapp) {

    GreeterTest = TestCase("GreeterTest");

    GreeterTest.prototype.testGreet = function() {
        var greeter = new myapp.Greeter();
        assertEquals("Hello World!", greeter.greet("World"));
    };

});

Notice :

  • the / before the tests
  • the fact that you have to reuse the "src/main/js" part ; I guess that is linked to the fact that my jsTestDriver config is located at the same level as the "src" folder, and it might needs some tweaking if placed otherwise.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top