简体   繁体   中英

Run Mocha specs from React app in a browser using Webpack and mocha-loader

I have a react App for which I'd like to run its mocha specs (unit-tests) in a browser. I found this SO post and tried to apply same idea to my project. I came up with the following webpack config file:

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',
            },
        ],
    },
};

And, after starting the server with:

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

I get the following error in console:

在此处输入图片说明

I've read that the problem might be with webpack-node-externals but not really sure what's happening. Any ideas?

I think you will want to use webpack-node-externals only when you bundle files for backend (as described in plugin README ). When you use it you forbid it to build all modules from node_modules folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM