Registry / testing / mocha-loader

mocha-loader

JSON →
library5.1.5jsnpmunverified

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-loader
INSTALL
IMPORT
SIG · MOCHA-LOADER
M
mocha-loader
testingjavascriptv5.1.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mocha-loader
import test from 'mocha-loader!./test.js'
const test = require('mocha-loader!./test.js')
Inline loader syntax works with both ESM and CJS, but the loader itself is webpack-specific and not a standard Node module.
mocha-loader
module.exports = { module: { rules: [ { test: /\.test\.js$/, use: 'mocha-loader' } ] } }
module.exports = { module: { loaders: [ { test: /\.test\.js$/, loader: 'mocha-loader' } ] } }
In webpack 4+, use 'rules' with 'use', not the deprecated 'loaders' array. 'loader' property still works but is less flexible.
mocha-loader
import 'mocha-loader!./test.js'
import test from './test.js'
If you want to run a test file as side-effect (no export), use an import without default binding. The loader runs the tests on bundle execution.

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.

// Install npm install --save-dev mocha-loader mocha webpack // webpack.config.js module.exports = { entry: './entry.js', output: { path: __dirname, filename: 'bundle.js' }, module: { rules: [ { test: /\.test\.js$/, use: 'mocha-loader', exclude: /node_modules/ }, ], }, }; // entry.js import './test.test.js'; // test.test.js describe('Test', () => { it('should pass', () => {}); }); // Build and run (in browser) // webpack // open index.html (which includes bundle.js)
Debug
Known issues
breakingv5.0.0 drops support for the 'node' webpack target; only works in browser-like environments
fix
If needed, use mocha directly (without webpack) for Node.js testing, or consider using mocha-webpack (alternative).
affects: >=5.0.0
breakingv4.0.0 drops support for Node.js <10.13 and Mocha <5
fix
Upgrade Node.js to >=10.13 and Mocha to ^5.0.0 or later.
affects: >=4.0.0
deprecatedThe package has not been updated since October 2020. Some users report compatibility issues with newer Mocha versions (>8).
fix
Consider using alternatives like karma-mocha-webpack or switch to a dedicated test runner (Jest, Vitest).
affects: >=5.1.5
gotchaThis loader runs tests in a browser context (simulated by webpack). Asynchronous operations like timers may behave differently than in Node.js.
fix
Ensure test code is browser-compatible or use a Node.js target alternative if needed.
affects: *
gotchaInline loader syntax (import 'mocha-loader!./test.js') does not work with webpack's 'entry' field; you must use a configuration with rules.
fix
Use webpack config rules as shown in the quickstart.
affects: *
Errors
Common errors & fixes
Error: Can't resolve 'mocha-loader'
Missing installation of mocha-loader or incorrect webpack configuration (not resolving node_modules).
fix
Run `npm install --save-dev mocha-loader mocha webpack` and ensure webpack.config.js has `resolveLoader` appropriately (usually defaults).
TypeError: mocha.run is not a function
Incompatible Mocha version (v9+ changed export structure). mocha-loader v5.1.5 only supports mocha up to v8.
fix
Downgrade mocha to v8: `npm install --save-dev mocha@8` or switch to a loader that supports Mocha 9+.
Cannot find module 'css-loader'
Obsolete dependency issue (mocha-loader v5.1.5 requires css-loader which is missing if not installed).
fix
Install css-loader as a devDependency: `npm install --save-dev css-loader` or use an older version of mocha-loader (but newer has fixes).
Error: webpack target 'node' is not supported by mocha-loader
Using mocha-loader v5+ with webpack target set to 'node'.
fix
Remove `target: 'node'` from webpack config, or switch to a different testing setup for Node.js tests (e.g., run mocha directly).
Upgrade
Version history
5.1.5latest on npm
Audit
Dependencies
mocharequiredPeer dependency: the test framework used to run tests
webpackrequiredPeer dependency: the module bundler that loads the tests
Agent activity
2 hits · last 30 days
node
2
Resources
mocha-loader — npm install mocha-loader · libregistry