Require.main.require works, but not inside the Mocha test

I wrote a global function to request specific files of my application / framework:

global.coRequireModel = function(name) { // CRASH happens here return require.main.require('./api/_co' + name + '/_co' + name + '.model'); } 

This module is located in / components / coGlobalFunctions.

The main app.js application requires the following:

 require('./components/coGlobalFunctions'); 

Then in other modules using β€œsomething” from the framework, I use:

 var baseScheme = coRequireModel('Base'); 

This works, but not in Mocha tests, which give me "Error: Cannot find module" immediately before calling require.main.require.

The test seems to come from a different source folder. But I thought that require.main.require would bring out the aspect of necessity regarding binding to modules.

EDIT:

An example of a test file living in api / user:

 var should = require('should'); var app = require('../../app'); var User = require('./user.model'); ... 
+8
unit-testing path require mocha
source share
3 answers

require.main points to a module that was launched directly from the node. So, if you run node app.js , then require.main will point to app.js If, on the other hand, you started it with mocha , then require.main will point to mocha. This is probably why your tests fail.

See site documentation for more details.

+7
source share

Since require.main was not index.html in my node -webkit application when running mocha tests, it threw errors on the left and right due to the inability to allow modules. The hacker fix in my test-helper.js (the first thing that was needed in all the tests is required) is fixed:

 var path = require('path') require.main.require = function (name) { // navigate to main directory var newPath = path.join(__dirname, '../', name) return require(newPath) } 

This seems wrong, although it worked. Is there a better way to fix this? This is similar to combining some of the above C # 7 solutions to get mocha to test work, but to modify main you just need to get everything working if the testing is really wrong.

For other ways to troubleshoot ".." - see here: https://gist.github.com/branneman/8048520

+5
source share

This is pretty old, but here is my solution.

I needed a test conductor module, which was published in a private registry and was required for the mocha test suite. I wanted the calling test code to pass the test code to the bundle, rather than requiring it directly:

 var harness = require('test-harness'); var codeUnderTest = harness('../myCode'); 

Inside the harness (which was found in the project directory of node_modules), I used the following code to request to find the correct file:

  if (!path.isAbsolute(target)) { target = path.join(path.dirname(module.parent.paths[0]), target); } var codeUndertest = require(target); ... return codeUnderTest; 

It depends on the required path resolution, which always starts with a search for the node_modules subdirectory relative to the calling file. Connect this to module.parent and you can access this search path. Then just remove the final part of node_modules and concatenate the relative file name.

For other scenarios that do not use relative paths, this can be done using the options parameter, which requires:

  var codeUndertest = require(target, {paths: module.parent.paths}); ... return codeUnderTest; 

And the two can also be combined. I used the first form because I actually used a proxyquire, which does not offer the paths option.

0
source share

All Articles