Is it possible to run js-test-driver tests that uses requirejs modules?

人盡茶涼 提交于 2019-12-11 08:34:25

问题


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


回答1:


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.


来源:https://stackoverflow.com/questions/12482072/is-it-possible-to-run-js-test-driver-tests-that-uses-requirejs-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!