SimulatedGREG / electron-vue

An Electron & Vue.js quick start boilerplate with vue-cli scaffolding, common Vue plugins, electron-packager/electron-builder, unit/e2e testing, vue-devtools, and webpack.
https://simulatedgreg.gitbooks.io/electron-vue/content/
MIT License
15.48k stars 1.55k forks source link

can not require node native modules in unit test #720

Open githoniel opened 6 years ago

githoniel commented 6 years ago

Found an issue or bug with electron-vue? Tell me all about it!

We use exteral to load native module in dev mode

externals: [
   ...Object.keys(dependencies || {}).filter(d => !whiteListedModules.includes(d))
],
output: {
    filename: '[name].js',
    libraryTarget: 'commonjs2', // ensure to compile require native module to `mdoule.exports = require('xxx')`, no `return 'xxx'`
    path: path.join(__dirname, '../dist/electron')
},
/// index.ejs
// add module globalPath
require('module').globalPaths.push('<%= htmlWebpackPlugin.options.nodeModules.replace(/\\/g, '\\\\') %>')

but in unit test, we do not have any config above

// karma.conf.js
delete webpackConfig.externals
delete webpackConfig.output.libraryTarget

So we can't load any native module in unit test.

Describe the issue / bug.

we can't load any native module in unit test.

How can I reproduce this problem?

'node-ffi' is a package design for node, can't compile by webpack.

  1. install 'node-ffi' as dependencies no devDependencies
  2. import this package wherever you like
  3. npm run unit

you will get error as unit test config did not treat 'node-ffi' as exterals

Tell me about your development environment.

My Solutions

// karma.conf.js
// don't treat dependencies as externals
delete webpackConfig.entry
// delete webpackConfig.externals
// delete webpackConfig.output.libraryTarget

preprocessors: {
   './index.js': ['electron', 'webpack', 'sourcemap']
},
// add this line in test/unit/index.js
require('module').globalPaths.push(require('path').resolve(__dirname, '../../node_modules'))

I don't know if this will break any thing.

tinyAdapter commented 5 years ago

I've faced the same issue. Thanks for your solution. 🌹