16

The answer to this question does not answer my question.

I'd like to load dependencies from outside my project root using the Intern as my testing framework. I'm currently working with the following directory structure:

www/
    project1/
        app/
        lib/
    project2/
        app/
        lib/
    intern-tests/
        node_modules/
        tests/
            functional/
                project1-tests/
                project2-tests/
            unit/
                project1-tests/
                project2-tests/
            intern.js
        Gruntfile.js

As you can see, I am making intern-tests its own project, and want this directory to hold all my tests for all of my projects. I've already set up my Gruntfile to execute tests with the grunt exec library converting the grunt projectName command to grunt test --project=projectName. All that works fine, but my unit tests cannot load the dependencies in the project1/ and project2/ directories.

For example, this is one of my unit tests:

define([
    'intern!object',
    'intern/chai!assert',
    'jquery',
    '../../../../project2/lib/js/var/document',
    '../../../../project2/lib/js/exports/file/functions/resizeInput'
], function(registerSuite, assert, $, document, resizeInput) {
    registerSuite({
        name: 'functions',
        resizeInput: function() {
            var $input = $(document.createElement('input'));
            resizeInput($input, 8, 20, 450, 200);
            assert.equal($input.width(), 450);
        }
    });
});

and running that test gives me the following error:

SUITE ERROR
Error: Failed to load module ../project2/lib/js/var/document from
project2/lib/js/var/document.js (parent: tests/unit/project2/functions)
at HTMLScriptElement.handler  <__intern\node_modules\dojo\loader.js:517:27>

How can I include these external files from my other projects?

Community
  • 1
  • 1
jperezov
  • 3,041
  • 1
  • 20
  • 36
  • Have you considered making those other projects submodules of the intern-test project or would that just make your intern-test project size too large? – Richard Nov 11 '15 at 22:05
  • @Richard the way everything is structured, they need to be separate projects (otherwise I'd just move my intern-tests Gruntfile to the `www` directory). – jperezov Nov 12 '15 at 15:54

2 Answers2

2

Maybe I'm thinking too simple, but I think of it like this:

  • project1 is a project
  • project2 is a project
  • intern_tests is a project

To achieve what you want to achieve:

cd /path/to/project1/
npm link

cd /path/to/project2/
npm link

cd /path/to/intern_tests/
npm install project1 project2

You now have a project structure like so:

intern_tests/
    node_modules/
        project1
        project2
    tests/
        unit/
        functional/
0

Alright, so until someone comes out with a better solution, I'm just going to create symlinks within my intern-tests/ project folder.

To do it on Windows, this looks like the following:

mklink /j "c:\www\intern-tests\project-symlinks\project2" c:\www\project2

which has the convention mklink /j "path\to\symlink" path\to\original (the /j is for junction)

On Mac / Linux, you would do the following:

ln -s /www/project2 /www/intern-tests/project-symlinks/project2

which has the convention ln -s /path/to/original /path/to/symlink (the -s is for symbolic)

This looks like the following:

www/
    project1/
        app/
        lib/
    project2/
        app/
        lib/
    intern-tests/
        project-symlinks/
            project1/
            project2/
        node_modules/
        tests/
            functional/
                project1-tests/
                project2-tests/
            unit/
                project1-tests/
                project2-tests/
            intern.js
        Gruntfile.js

And changes my test to the following:

define([
    'intern!object',
    'intern/chai!assert',
    'jquery',
    '../../../project-symlinks/project2/lib/js/var/document',
    '../../../project-symlinks/project2/lib/js/exports/file/functions/resizeInput'
], function(registerSuite, assert, $, document, resizeInput) {
    registerSuite({
        name: 'functions',
        resizeInput: function() {
            var $input = $(document.createElement('input'));
            resizeInput($input, 8, 20, 450, 200);
            assert.equal($input.width(), 450);
        }
    });
});
Wilgert
  • 706
  • 1
  • 6
  • 23
jperezov
  • 3,041
  • 1
  • 20
  • 36
  • 2
    This is potentially fine if you're the only one ever working on your code, otherwise you'd have to onboard people with this process. Even still, what if you move your directory around or change the name? Honestly if these need to be separate directories, you should consider hosting them somewhere, such as github or as npm modules, then requiring them in to your project via requirejs / browserify / webpack / whatever dependency manager you prefer. Whenever a project has to modularize outside of its root directory, using dependency management is the logical next step. – dcochran Nov 13 '15 at 15:57