mocha-webpack
raw JSON → 1.1.0 verified Sat Apr 25 auth: no javascript deprecated
A CLI tool that integrates Mocha test runner with Webpack bundling, allowing you to test modules that rely on Webpack features (loaders, plugins, resolve aliases) without pre-building or using hacks. Version 1.1.0 is the latest stable release (as of late 2018). It supports Mocha >=2.4.5 <=5 and Webpack 2.x/3.x. Key differentiators: bundling tests into a single bundle with Webpack's full pipeline (loaders, resolve, plugins), built-in source map support, memory-fs for speed (no disk writes), customizable file watcher, and support for multiple entry files/patterns. The project is effectively unmaintained (last release 2017) for Webpack 4+ compatibility.
Common errors
error Error: Cannot find module 'mocha-webpack' from '<PROJECT>' at Function.module.exports [as runMain] (module.js:xxx:xx) ↓
cause Mocha-webpack is not installed globally or locally when running via CLI.
fix
Install mocha-webpack locally:
npm install --save-dev mocha-webpack and use npx or package.json scripts. error Error: webpack.optimize.OccurrenceOrderPlugin is not a constructor ↓
cause Using mocha-webpack with webpack 4, which removed deprecated plugins.
fix
Downgrade to webpack@3 or migrate to mochapack.
error TypeError: Cannot read property 'getFiles' of undefined ↓
cause This error appears in v1.0.1 and was fixed; related to file resolution issues.
fix
Update to mocha-webpack@1.1.0 which fixed this bug.
error Error: Module not found: Error: Can't resolve 'fs' in '<PROJECT>/node_modules/mocha-webpack' ↓
cause Webpack target/node misconfiguration; mocha-webpack expects Node environment.
fix
Set webpack target to 'node' in your webpack config:
target: 'node'. Warnings
deprecated mocha-webpack is unmaintained since v1.1.0 (2017). No Webpack 4/5 compatibility. Use mochapack or webpack-shell-plugin instead. ↓
fix Migrate to mochapack (https://github.com/sysgears/mochapack) or configure webpack+mocha manually.
breaking v1.0.0 switched from writing bundle to disk to using memory-fs. If you relied on the generated bundle file, it no longer exists. ↓
fix Remove any references to generated bundle file. Use --webpack-config to specify output settings or rely on memory-fs.
breaking v1.0.0 replaced 'glob-like' file resolution with real glob. Patterns like 'test/unit/*' may resolve differently if `resolve.extensions` not set. ↓
fix Ensure webpack resolve.extensions includes '.js' and other extensions used in your tests.
gotcha mocha-webpack will ignore webpack's `entry` and `output` configuration; it overrides them internally. Do not set them in your webpack config, or they will be overwritten. ↓
fix Omit `entry` and `output` from your webpack test config, or use `--webpack-config` that merges partial configs.
gotcha Webpack 4 is not supported. Attempting to use mocha-webpack with webpack@4 will fail at module resolution. ↓
fix Use webpack@3.x or switch to mochapack (supports webpack 4+).
deprecated The `--opts` flag defaults to looking for `mocha-webpack.opts` or `mocha.opts` in the current directory. If you have both, mocha-webpack prefers `mocha-webpack.opts`. ↓
fix Rename your opts file to `mocha-webpack.opts` or use `--opts path/to/your.opts`.
Install
npm install mocha-webpack yarn add mocha-webpack pnpm add mocha-webpack Imports
- mocha-webpack wrong
mocha-webpack --require source-map-support/register test/**/*.spec.jscorrectnpx mocha-webpack --webpack-config webpack.config.js test/**/*.spec.js - mocha-webpack (require from Node) wrong
const { run } = require('mocha-webpack');correctimport 'mocha-webpack'; // or require('mocha-webpack') — only side effects
Quickstart
// package.json scripts
{
"scripts": {
"test": "mocha-webpack --webpack-config webpack.test.js --opts ./mocha-webpack.opts test/**/*.spec.js"
}
}
// mocha-webpack.opts
--timeout 10000
--reporter spec
// webpack.test.js (example)
const path = require('path');
module.exports = {
mode: 'development',
resolve: {
extensions: ['.js', '.jsx', '.json']
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { presets: ['@babel/preset-env', '@babel/preset-react'] }
}
}
]
}
};
// Run: npx mocha-webpack --webpack-config webpack.test.js test/**/*.spec.js