2015-11-03 70 views
16

this question的回答不能回答我的问题。在实习生的项目目录之外加载依赖项

我想加载从我的项目根外使用实习生作为我的测试框架的依赖项。我目前有以下目录结构的工作:

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> 

我怎么能包括我的其他项目,这些外部文件?

+0

你有没有考虑让那些其他项目的子模块实习生测试项目还是会让你的实习生项目太大? – Richard

+0

@Richard一切都是结构化的,他们需要成为单独的项目(否则我只需将我的实习生Gruntfile移动到“www”目录)。 – jperezov

回答

0

好的,所以直到有人提出更好的解决方案,我只是要在我的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); 
     } 
    }); 
}); 
+2

如果您是有史以来唯一一个处理您的代码的人,那么这可能很好,否则您将不得不让这些人参与这个过程。即使如此,如果您移动目录或更改名称,该怎么办?老实说,如果这些需要分开的目录,你应该考虑将它们托管在某处,例如github或npm模块,然后通过requirejs/browserify/webpack /你喜欢的任何依赖关系管理器来请求它们进入你的项目。每当项目需要在其根目录之外进行模块化时,使用依赖管理是下一步的合理步骤。 – dcochran

2

也许我想得简单,但我认为它是这样的:

  • 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/