A webpack loader that allows Mocha tests to be bundled and run via webpack. Current stable version is 5.1.5 (October 2020), supporting mocha ^5.0.0 to ^8.0.0 and webpack ^4.0.0 || ^5.0.0. It is maintained under the webpack-contrib organization. Key differentiator: integrates Mocha testing into the webpack build pipeline, enabling module resolution, loaders, and plugins for test files. No options are needed; simply apply the loader to test files. Note: v5.0.0 dropped support for the node target (only works in browser-like environments). Release cadence is sporadic; last release over 3 years ago.
npm install mocha-loaderVerified import paths — ran on the pinned version, not inferred.
Shows a minimal setup: install dependencies, configure webpack rule, write a test file, and build. Tests run when the bundle is loaded in a browser.
If needed, use mocha directly (without webpack) for Node.js testing, or consider using mocha-webpack (alternative).
Upgrade Node.js to >=10.13 and Mocha to ^5.0.0 or later.
Consider using alternatives like karma-mocha-webpack or switch to a dedicated test runner (Jest, Vitest).
Ensure test code is browser-compatible or use a Node.js target alternative if needed.
Use webpack config rules as shown in the quickstart.
Run `npm install --save-dev mocha-loader mocha webpack` and ensure webpack.config.js has `resolveLoader` appropriately (usually defaults).
Downgrade mocha to v8: `npm install --save-dev mocha@8` or switch to a loader that supports Mocha 9+.
Install css-loader as a devDependency: `npm install --save-dev css-loader` or use an older version of mocha-loader (but newer has fixes).
Remove `target: 'node'` from webpack config, or switch to a different testing setup for Node.js tests (e.g., run mocha directly).