enhanced-require
enhanced-require copied to clipboard
Mocha node testing with enhanced requires
I'm doing unit testing in node with mocha. Everything works smoothly but when I hit the code that uses require.context() I get an exception:
TypeError: Object function require(path) {
return self.require(path);
} has no method 'context'
I've plugged in the enhanced-require to get around the problem. It works perfectly with my own code, but some modules fail to load with it. For example when I do this:
var webpackConfig = require('./webpack.config');
require = require('enhanced-require')(module, {
recursive: true,
resolve: webpackConfig.resolve
});
var chalk = require('chalk');
var gutil = require('gulp-util');
var moment = require('moment');
var path = require('path');
var watch = require('gulp-watch');
var jsdom = require('jsdom');
var mocha = require('gulp-mocha');
I get this:
/project/build/node_modules/jsdom/package.json:2
"name": "jsdom",
^
SyntaxError: Unexpected token :
at NormalModuleMixin.Module._compile (/project/deps/node_modules/enhanced-require/lib/Module.js:52:24)
at RequireContext.theRequire (/project/deps/node_modules/enhanced-require/lib/RequireContext.js:169:10)
at /project/build/node_modules/jsdom/lib/jsdom.js:4:11
at NormalModuleMixin.Module._compile (/project/deps/node_modules/enhanced-require/lib/Module.js:53:18)
at RequireContext.theRequire (/project/deps/node_modules/enhanced-require/lib/RequireContext.js:169:10)
at Object.<anonymous> (/project/build/gulp-test.js:21:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
Or if I move require('gulp-mocha') above require('jsdom'), I get this:
TypeError: Cannot call method 'push' of undefined
at /project/build/node_modules/mocha/lib/mocha.js:27:16
at NormalModuleMixin.Module._compile (/project/deps/node_modules/enhanced-require/lib/Module.js:53:18)
at RequireContext.theRequire (/project/deps/node_modules/enhanced-require/lib/RequireContext.js:169:10)
at /project/build/node_modules/mocha/index.js:4:5
at NormalModuleMixin.Module._compile (/project/deps/node_modules/enhanced-require/lib/Module.js:53:18)
at RequireContext.theRequire (/project/deps/node_modules/enhanced-require/lib/RequireContext.js:169:10)
at /project/build/node_modules/gulp-mocha/index.js:5:13
at NormalModuleMixin.Module._compile (/project/deps/node_modules/enhanced-require/lib/Module.js:53:18)
at RequireContext.theRequire (/project/deps/node_modules/enhanced-require/lib/RequireContext.js:169:10)
at Object.<anonymous> (/project/build/gulp-test.js:21:13)
It appears that some modules either have little bugs or use not every day functionality of the require, which enhanced require cannot handle. I'm wondering if it's possible to use it as a complete replacement of the standard require() in node?
For the first error you need to use the json-loader.
The second one is here: module.paths is not supported (is not even documented in the node.js documentation and it's not good style).
I would love to have more time to maintain enhanced-require, but webpack gets more traction and eats up my whole time...
You also need to add extensions: ['', '.js', '.json'], to your webpack config. I'm also having problems on node.js... Not easy to fix.
Interesting approach @ianbytchek. I was running into a similar issue around require.context when trying to run mocha tests on the browser. The command I ran was node node_modules/enhanced-require/bin/enhanced-require.js "mocha\!./test/index.js" and it errors with:
node node_modules/enhanced-require/bin/enhanced-require.js "mocha\!./test/index.js"
TypeError: Object #<Object> has no method 'ui'
at Object.Mocha (/Users/dmarr/src/webpack-with-common-libs/node_modules/mocha/lib/mocha.js:83:8)
at WEBPACK_CORE_LOADER_EXECUTION (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/webpack-core/lib/NormalModuleMixin.js:131:71)
at runSyncOrAsync (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/webpack-core/lib/NormalModuleMixin.js:131:93)
at nextLoader (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/webpack-core/lib/NormalModuleMixin.js:233:3)
at Storage.finished (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at SyncNodeJsInputFileSystem.readFile (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/enhanced-resolve/lib/SyncNodeJsInputFileSystem.js:19:4)
at Storage.provide (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:56:2)
at CachedInputFileSystem.readFile (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:134:24)
at NormalModuleMixin.onLoadPitchDone (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/webpack-core/lib/NormalModuleMixin.js:204:7)
at NormalModuleMixin.loadPitch (/Users/dmarr/src/webpack-with-common-libs/node_modules/enhanced-require/node_modules/webpack-core/lib/NormalModuleMixin.js:157:27)
I guess this is mocha-loader's fault but i'm not certain. I filed a bug there: https://github.com/webpack/mocha-loader/issues/17