Tuesday, 30 May 2017

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:

enter image description here

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



via leo.fcx

No comments:

Post a Comment