3

My project follows the following (simplified) directory structure:

\
|- app
   |- script1.js
   |- script2.js
|- test
   |- intern.conf.js
   |- test.spec.js 

I'm using requirejs in my application and thus all scripts under app/ directory have their dependencies relative to that folder.

Because Intern baseUrl defaults to the root folder, the scripts under app/ fail to load.

However, setting the baseUrl under loader to 'app' or '/app' and so forth, results in failure to load the test suite..

Error: Failed to load module ../test/intern.conf from test/intern.conf.js (parent: *2)

I tried to set the test suite location to '../test/test.spec.js' and so forth, with no success.

Filipe Miguel Fonseca
  • 6,400
  • 1
  • 31
  • 26

2 Answers2

2

The baseUrl must be the base URL common to all modules, including test modules, so in your case would be the parent directory of the app and test directories. Normally this means that you will cd to the parent directory and simply run Intern from there, like intern-runner config=test/intern.conf, with no additional loader configuration necessary.

If you do need extra configuration (for example, to define app as a package), the loader configuration in your Intern configuration file doesn’t need to be the same as the loader configuration in your application, so in practice any difference between the two should never be an issue. You will have one configuration in your application entrypoint that works for your app, and one configuration in your test configuration that works for your test environment.

Relative AMD module IDs are relative to the module itself, so if your module app/script1 has a dependency ./script2, it will correctly load /root/app/script2.js, not /root/script2.js. When you load app/script1 from your test/test.spec module, so long as your baseUrl is the parent directory, you can either require ../app/script1 (if this makes sense, i.e. if the two are part of the same logical package) or app/script1 (if test and app are supposed to be two different packages).

C Snover
  • 17,908
  • 5
  • 29
  • 39
  • My issue is exactly loading `script2` from `script1`, cause in the browser console I have the following error message: `Error: Error: Failed to load module script2 from /script2.js (parent: app/script1) at http://localhost:9000/__intern/node_modules/dojo/dojo.js:731` – Filipe Miguel Fonseca Jul 08 '14 at 06:18
  • Using a relative path for script2 does fix the test, but is it good practice to do so? – Filipe Miguel Fonseca Jul 08 '14 at 06:26
  • 1
    You should always use relative module IDs for modules that are part of the same package. This is a cornerstone of module portability. As you see, without it, your code is completely non-portable and cannot ever be moved from exactly where it’s expected without being totally broken. – C Snover Jul 08 '14 at 15:37
0

I believe you may need to setup some paths in your requirejs configuration object. It will allow you to load in scripts outside of the baseUrl directory. So, you can still setup the baseUrl to be "app" while loading in your test directory though a path alias (http://requirejs.org/docs/api.html#jsfiles ).

In the example below both "app" and "test" are at the root along with the requirejs.config call, so using "/test" works. You could alternatively use "../" should files not be in the root.

requirejs.config({
    baseUrl: "/app",
    paths: {
        "test"               : "/test"
    },
});

This would allow you to require a test file through:

define(["test/test.spec.js"], function(){ ... });

as it will use the "test" path to find out where that directory is.

  • The issue is the intern-runner seems to be using the baseUrl too, in particular, setting the baseUrl to '/app' introduces an error in the console: `Error: Failed to load module ../Users/miguel/project/test/intern.runner.conf from Users/miguel/project/test/intern.runner.conf.js (parent: *2)` – Filipe Miguel Fonseca Jul 07 '14 at 21:37
  • Sorry, I misunderstood intern for being a filename and not another AMD technology. I believe the `useLoader` would help with this issue after looking at intern. Sources: https://github.com/theintern/intern/wiki/Using-Intern-with-Alternative-AMD-Loaders-(like-RequireJS) https://github.com/theintern/intern/wiki/Configuring-Intern#user-content-useloader – morriswchris Jul 07 '14 at 22:12