【问题标题】:Loading dependencies outside of project directory in the Intern在 Intern 中加载项目目录之外的依赖项
【发布时间】:2016-02-03 23:46:42
【问题描述】:

this question 的答案没有回答我的问题。

我想使用 Intern 作为我的测试框架从我的项目根目录外部加载依赖项。我目前正在使用以下目录结构:

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

如您所见,我正在创建 intern-tests 自己的项目,并希望此目录保存 所有我对 所有我的项目的测试。我已经设置了我的 Gruntfile 以使用 grunt exec 库执行测试,将 grunt projectName 命令转换为 grunt test --project=projectName。一切正常,但我的单元测试无法加载 project1/project2/ 目录中的依赖项。

例如,这是我的单元测试之一:

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

并运行该测试给我以下错误:

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>

如何从我的其他项目中包含这些外部文件?

【问题讨论】:

  • 您是否考虑过将其他项目作为实习生测试项目的子模块,或者这只会使您的实习生测试项目规模过大?

标签: javascript unit-testing dojo amd intern


【解决方案1】:

也许我想得太简单了,但我是这样想的:

  • project1 是一个项目
  • project2 是一个项目
  • intern_tests 是一个项目

实现你想要达到的目标:

cd /path/to/project1/
npm link

cd /path/to/project2/
npm link

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

你现在有一个像这样的项目结构:

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

【讨论】:

    【解决方案2】:

    好的,所以在有人提出更好的解决方案之前,我将在我的 intern-tests/ 项目文件夹中创建符号链接。

    要在 Windows 上执行此操作,如下所示:

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

    具有约定mklink /j "path\to\symlink" path\to\original(/j 表示连接)

    在 Mac / Linux 上,您将执行以下操作:

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

    具有约定ln -s /path/to/original /path/to/symlink(-s 表示符号)

    如下所示:

    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
    

    并将我的测试更改为以下内容:

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

    【讨论】:

    • 如果你是唯一一个处理你的代码的人,这可能很好,否则你必须让人们参与这个过程。即使如此,如果您移动目录或更改名称怎么办?老实说,如果这些需要单独的目录,您应该考虑将它们托管在某个地方,例如 github 或作为 npm 模块,然后通过 requirejs / browserify / webpack / 任何您喜欢的依赖管理器将它们引入您的项目。每当项目必须在其根目录之外进行模块化时,使用依赖管理是合乎逻辑的下一步。
    猜你喜欢
    • 2014-05-15
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多