2

我正在尝试使用 Iron-node (v2.2.17) 来调试我的 mocha 单元测试。当我从 package.json 运行此命令时,单元测试运行良好:

"test": "cross-env NODE_ENV=test mocha test/.setup.js --reporter progress --compilers js:babel-core/register --require babel-polyfill --recursive \"./src/**/*.spec.js\" \"./src/**/*.integrationSpec.js\" \"./test/**/*.spec.js\"",

但是,当我运行此命令时,测试失败:

"debug:test": "cross-env NODE_ENV=test iron-node node_modules\\mocha\\bin\\_mocha test/.setup.js --reporter progress --compilers js:babel-core/register --require babel-polyfill --recursive \"./src/**/*.spec.js\" \"./src/**/*.integrationSpec.js\" \"./test/**/*.spec.js\"",

控制台中的错误是:

Error: Cannot find module 'src/framework/api/entityAddresses/entityAddressesAc  tions'

  - module.js:16 require
    internal/module.js:16:19

  - entityAddressesActions.spec.js:5 Object.<anonymous>
    entityAddressesActions.spec.js:5:1

该模块位于C:\TFS\Dev-UI\WebApp\Src\Web\Web\src\framework\api\entityAddresses\entityAddressesActions.js

我的项目文件夹是C:\TFS\Dev-UI\WebApp\Src\Web\Web,我process.env.NODE_PATH的也是C:\TFS\Dev-UI\WebApp\Src\Web\Web。(它是未定义的,但我将其设置为process.cwd()我的.iron-node.js- 请参阅https://github.com/sa/iron-node/issues/98#issuecomment-218658236

我无法弄清楚为什么找不到该模块。我错过了一些配置吗?

编辑

这个 SO 答案建议process.env.NODE_PATH应用程序启动后设置将不起作用,因为模块路径已缓存:Determine project root from a running node.js application

我不知道如何在铁节点开始之前设置它,它总是未定义

4

1 回答 1

1

问题可以通过 .iron-node.js 配置文件解决:

process.env.NODE_PATH = process.cwd(); // fix problem where modules are not resolved - Jack Allan.
var path = require("path");
var settings = {
  "nodeModule" : {
     "scriptInjection" : "module.paths.push(process.env.NODE_PATH);",  // fix problem where modules are not resolved - Jack Allan.
  },
  "v8": {
    "flags" : [                             // DEFAULT=[]; https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md
    ]
  },
  "app": {
    "native+"               : false,   // DEFAULT=FALSE; extends require to search native modules respecting the current v8 engine version.
    "autoAddWorkSpace"      : false,  // DEFAULT=TRUE; disables the autoAddWorkSpace behavior.
    "openDevToolsDetached"  : false,  // DEFAULT=FALSE; opens the dev tools windows detached in an own window.
    "hideMainWindow"        : false,  // DEFAULT=FALSE;  hides the main window to show dev tools only.
  },
  "workSpaceDirectory"        : function(argv) {  // determines the workspace directory for specific commandline applications.
    var result = "";
    if (argv[2]){
      result = path.dirname(argv[2]);
      var startupScriptName = path.basename(argv[2]).toLowerCase();

      switch(startupScriptName) {
          case "_mocha":
            result = process.cwd();
            break;
          default:
            result = path.resolve(result);
            break;
      }
    }

    return result;
  }
};

module.exports = settings;

感谢 sa 的建议:https ://github.com/sa/iron-node/issues/98#issuecomment-218712907

于 2016-05-12T11:00:51.713 回答