mocha-webpack icon indicating copy to clipboard operation
mocha-webpack copied to clipboard

Using webpack-node-externals causes tests to be unable to load modules

Open ELLIOTTCABLE opened this issue 6 years ago • 0 comments

So, I'm invoking MochaWebpack programmatically, and trying to use webpack-node-externals as suggested in the documentation. Something like this:

// Tell Webpack to compile for Node.js (and thus Mocha); and to ignore node_modules.
// See: <http://zinserjan.github.io/mocha-webpack/docs/installation/webpack-configuration.html>
target: 'node',
externals: [nodeExternals({
  whitelist: [/^@ourCompanyScope/]
})],

However, then I get errors like this from our test-setup code:

 WEBPACK  Compiled successfully in 2524ms

 MOCHA  Testing...

 RUNTIME EXCEPTION  Exception occurred while loading your tests

Error: Cannot find module 'chai'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function._resolveFilename (/Users/ec/Work/gulp-registry/node_modules/mocha-webpack/lib/util/registerRequireHook.js
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.chai (/dist/webpack:/kidscript/external "chai":1:1)
    at __webpack_require__ (/dist/webpack:/kidscript/webpack/bootstrap:19:1)
    at Module.require (/dist/webpack:/kidscript/test/setup/index.js:1:12)
    at __webpack_require__ (/dist/webpack:/kidscript/webpack/bootstrap:19:1)
    at Object.../gulp-registry/node_modules/mocha-webpack/lib/entry.js (/dist/webpack:/kidscript/gulp-registry/node_modul
    at __webpack_require__ (/dist/webpack:/kidscript/webpack/bootstrap:19:1)
    at /dist/webpack:/kidscript/webpack/bootstrap:83:1
    at /dist/kidscript.js:97:10
    at webpackUniversalModuleDefinition (/dist/webpack:/kidscript/webpack/universalModuleDefinition:3:1)
    at Object.<anonymous> (/dist/webpack:/kidscript/webpack/universalModuleDefinition:10:2)
    at Module._compile (module.js:652:30)
    at Object._module2.default._extensions.(anonymous function) [as .js] (/Users/ec/Work/gulp-registry/node_modules/mocha
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at /Users/ec/Work/gulp-registry/node_modules/mocha/lib/mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/ec/Work/gulp-registry/node_modules/mocha/lib/mocha.js:247:14)
    at Mocha.run (/Users/ec/Work/gulp-registry/node_modules/mocha/lib/mocha.js:576:10)
    at /Users/ec/Work/gulp-registry/node_modules/mocha-webpack/lib/runner/TestRunner.js:191:27
    at /Users/ec/Work/gulp-registry/node_modules/mocha-webpack/lib/webpack/compiler/registerReadyCallback.js:26:7
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/ec/Work/gulp-registry/node_modules/tapable/lib/HookCod
    at AsyncSeriesHook.lazyCompileHook (/Users/ec/Work/gulp-registry/node_modules/tapable/lib/Hook.js:154:20)
    at emitRecords.err (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:251:22)
    at Compiler.emitRecords (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:366:39)
    at emitAssets.err (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:245:10)
    at hooks.afterEmit.callAsync.err (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:352:14)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/ec/Work/gulp-registry/node_modules/tapable/lib/HookCod
    at AsyncSeriesHook.lazyCompileHook (/Users/ec/Work/gulp-registry/node_modules/tapable/lib/Hook.js:154:20)
    at asyncLib.forEach.err (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:349:27)
    at done (/Users/ec/Work/gulp-registry/node_modules/neo-async/async.js:2854:11)
    at /Users/ec/Work/gulp-registry/node_modules/neo-async/async.js:2805:7
    at MemoryFileSystem.writeFile (/Users/ec/Work/gulp-registry/node_modules/memory-fs/lib/MemoryFileSystem.js:328:9)
    at writeOut (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:333:29)
    at asyncLib.forEach (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:343:7)
    at baseEach (/Users/ec/Work/gulp-registry/node_modules/neo-async/async.js:2416:9)
    at Object.each (/Users/ec/Work/gulp-registry/node_modules/neo-async/async.js:2843:9)
    at emitFiles (/Users/ec/Work/gulp-registry/node_modules/webpack/lib/Compiler.js:306:13)
    at Immediate.<anonymous> (/Users/ec/Work/gulp-registry/node_modules/memory-fs/lib/MemoryFileSystem.js:288:4)

Do I misunderstand how webpack-node-externals is intended to work, with this project? If I remove it, my tests compile and pass just fine.

(By the way, this project is frikken cool; and probably saved me something like a couple weeks' work setting up exactly this workflow within our company. Also, I'm maybe the only person using your project as an API instead of thru the CLI — thanks for doing your best to clean up and export a sane interface from TestRunner! At least one person, me, is very appreciative!)

ELLIOTTCABLE avatar Dec 14 '18 00:12 ELLIOTTCABLE