在浏览器中使用Webpack和mocha-loader从React应用程序运行Mocha规范

我有一个应用程序,我想在浏览器中运行它的mocha规格(unit testing)。 我发现这个SOpost,并试图将相同的想法应用到我的项目。 我想出了下面的webpackconfiguration文件:

webpack.config.test.js

 const nodeExternals = require('webpack-node-externals'); const path = require('path'); const host = 'localhost'; const port = '8084'; module.exports = { target: 'web', externals: [nodeExternals()], entry: './specs/index.js', output: { filename: 'debug.bundle.js', path: path.join(__dirname, 'tests'), publicPath: `http://${host}:${port}/tests`, }, devServer: { host, port, }, module: { rules: [ { test: /\.js$/, loaders: ['react-hot-loader', 'babel-loader'], enforce: 'pre', }, { test: /.+Spec\.js$/, loaders: ['mocha-loader'], }, { test: /(\.css|\.scss)$/, loader: 'null-loader', exclude: [ /build/, ], }, { test: /(\.jpg|\.jpeg|\.png|\.gif)$/, loader: 'null-loader', }, ], }, }; 

而且,在启动服务器之后:

 webpack-dev-server --config webpack.config.test.js 

在控制台中出现以下错误:

在这里输入图像描述

我读过的问题可能是与webpack-node-externals但不知道发生了什么。 有任何想法吗?

我想只有在你为后端打包文件时才会使用webpack-node-externals (如插件自述文件中所述)。 当您使用它时,禁止从node_modules文件夹构build所有模块。