Loading tests from different directories doesn't quite work

12 views
Skip to first unread message

Roman Snitko

unread,
Jun 3, 2012, 1:38:35 PM6/3/12
to jsclas...@googlegroups.com
Here's what I'm trying to accomplish:

  // file: test/runner.js

  JS.Packages(function() { with(this) {
    autoload(/.*Spec$/, {from: 'test/specs/model' });
    autoload(/.*Spec$/, {from: 'test/specs/lib'      });
  }});

  JS.require('JS.Test', function() {
    var tests = ['ModelSpec', 'ValidatableSpec', 'ErrorsStackSpec', 'EnvironmentSpec'];
    tests.push(JS.Test.method('autorun'));
    JS.require.apply(JS, tests); 
  });

The result is this: Cannot find module './test/specs/model/environment_spec'
If I comment out the first autoload and remove everything except EnvironmentSpec from the tests array, it works - but obviously only running EnvironmentSpec.

So it looks like JS.require is only talking the first autoload seriously. Not sure what's going on.

James Coglan

unread,
Jun 5, 2012, 11:33:27 AM6/5/12
to jsclas...@googlegroups.com
JS.require() will use the *first* autoload rule that matches, and no others. This is because trying multiple rules would involved trying to load the module from multiple places, which is not a great idea on the Web. In works on local files, where you can check if a file exists relatively cheaply, but not on the Web. Here's what I do in this situation.

First, one autoload rule for my specs:

    autoload(/.*Spec$/, {from: 'test/specs' });

Then, keep all your spec files where they are, but make the modules they contain match their path, so e.g. test/specs/model/environment_spec names its test suite Model.EnvironmentSpec rather than just EnvironmentSpec. Then the call to JS.require('Model.EnvironmentSpec') will load test/specs/model/environment_spec.js.

Roman Snitko

unread,
Jun 5, 2012, 10:23:11 PM6/5/12
to jsclas...@googlegroups.com
 Then the call to JS.require('Model.EnvironmentSpec') will load test/specs/model/environment_spec.js.

Thanks James, that indeed worked. However I must add that one must first define an object that would contain each module, for example I had to do this in runner.js:

  JS.ENV.Model = {}
  JS.ENV.Lib     = {}

Personally, I think this might not be a very elegant solution, because the names make it look as if those are actual classes, not namespaces for tests. On the other hand, if I decided to call it, say, JS.ENV.ModelTests then I would have to call the directory /tests/specs/model_tests, which doesn't make too much sense.

I guess it's my OCD talking, but maybe a more elegant solution here is possible in the future. Or maybe I'm not very familiar with JS.Class yet.
Reply all
Reply to author
Forward
0 new messages