简体   繁体   中英

Loading dependencies outside of project directory in the Intern

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?

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/

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);
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM