2

I am trying to use iron-node (v2.2.17) to debug my mocha unit-tests. The unit-tests run fine when I run this command from my 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\"",

However when I run this command the tests fail:

"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\"",

The error in the console is:

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

This module is located in C:\TFS\Dev-UI\WebApp\Src\Web\Web\src\framework\api\entityAddresses\entityAddressesActions.js

My project folder is C:\TFS\Dev-UI\WebApp\Src\Web\Web and my process.env.NODE_PATH is also C:\TFS\Dev-UI\WebApp\Src\Web\Web. (it was undefined but I set it to process.cwd() in my .iron-node.js - see https://github.com/s-a/iron-node/issues/98#issuecomment-218658236)

I can't figure out why the module is not found. Am I missing some configuration?

edit

This SO answer suggests setting process.env.NODE_PATH once the app has started won't work because the module paths are cached: Determine project root from a running node.js application

I am at a loss as to how to set this before iron-node starts, it is always undefined

Community
  • 1
  • 1
Jack Allan
  • 14,554
  • 11
  • 45
  • 57

1 Answers1

1

Problem can be resolved with an .iron-node.js config file:

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;

Thanks to s-a who suggested this: https://github.com/s-a/iron-node/issues/98#issuecomment-218712907

Jack Allan
  • 14,554
  • 11
  • 45
  • 57