Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require.main.require works but not inside Mocha test

I have written a global function for requiring certain files of my app/framework:

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

This module is in /components/coGlobalFunctions.

It is required in my main app app.js like this:

require('./components/coGlobalFunctions');

Then in other modules using "something" from the framework I use:

var baseScheme = coRequireModel('Base');

This works but not in the Mocha tests which give me a "Error: Cannot find module" right before the require.main.require call.

It seems that the test is coming from another source folder. But I thought the require.main.require would take out the aspect of having to relatively linking to modules.

EDIT:

An example test file living in api/user:

var should = require('should');
var app = require('../../app');
var User = require('./user.model');
...
like image 613
Matthias Max Avatar asked May 05 '15 08:05

Matthias Max


People also ask

What is require main === module?

When a file is run directly from Node. js, require. main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require. main === module .

What is a pending test in mocha technically?

A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO. For Mocha, the documentation says that a pending test is a test without any callback.

How do I specify a test file in mocha?

The nice way to do this is to add a "test" npm script in package. json that calls mocha with the right arguments. This way your package. json also describes your test structure.


2 Answers

require.main points to the module that was run directly from node. So, if you run node app.js, then require.main will point to app.js. If, on the other hand, you ran it using mocha, then require.main will point to mocha. This is likely why your tests are failing.

See the node docs of more details.

like image 172
JME Avatar answered Oct 03 '22 22:10

JME


Because require.main was not index.html in my node-webkit app when running mocha tests, it threw errors left and right about not being able to resolve modules. Hacky fix in my test-helper.js (required first thing in all tests) fixed it:

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

This feels wrong, though it worked. Is there a better way to fix this? It's like combining some of the above solutions with #7 to get mocha testing working, but modifying main's require just to make everything work when testing feels really wrong.

For other avoid-the-".."-mess solutions, see here: https://gist.github.com/branneman/8048520

like image 30
Emily Avatar answered Oct 03 '22 22:10

Emily