{"id":20218,"library":"mocha-loader","title":"mocha-loader","description":"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.","status":"maintenance","version":"5.1.5","language":"javascript","source_language":"en","source_url":"https://github.com/webpack-contrib/mocha-loader","tags":["javascript","webpack","mocha"],"install":[{"cmd":"npm install mocha-loader","lang":"bash","label":"npm"},{"cmd":"yarn add mocha-loader","lang":"bash","label":"yarn"},{"cmd":"pnpm add mocha-loader","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: the test framework used to run tests","package":"mocha","optional":false},{"reason":"Peer dependency: the module bundler that loads the tests","package":"webpack","optional":false}],"imports":[{"note":"Inline loader syntax works with both ESM and CJS, but the loader itself is webpack-specific and not a standard Node module.","wrong":"const test = require('mocha-loader!./test.js')","symbol":"mocha-loader","correct":"import test from 'mocha-loader!./test.js'"},{"note":"In webpack 4+, use 'rules' with 'use', not the deprecated 'loaders' array. 'loader' property still works but is less flexible.","wrong":"module.exports = { module: { loaders: [ { test: /\\.test\\.js$/, loader: 'mocha-loader' } ] } }","symbol":"mocha-loader","correct":"module.exports = { module: { rules: [ { test: /\\.test\\.js$/, use: 'mocha-loader' } ] } }"},{"note":"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.","wrong":"import test from './test.js'","symbol":"mocha-loader","correct":"import 'mocha-loader!./test.js'"}],"quickstart":{"code":"// Install\nnpm install --save-dev mocha-loader mocha webpack\n\n// webpack.config.js\nmodule.exports = {\n  entry: './entry.js',\n  output: { path: __dirname, filename: 'bundle.js' },\n  module: {\n    rules: [\n      { test: /\\.test\\.js$/, use: 'mocha-loader', exclude: /node_modules/ },\n    ],\n  },\n};\n\n// entry.js\nimport './test.test.js';\n\n// test.test.js\ndescribe('Test', () => {\n  it('should pass', () => {});\n});\n\n// Build and run (in browser)\n// webpack\n// open index.html (which includes bundle.js)","lang":"javascript","description":"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."},"warnings":[{"fix":"If needed, use mocha directly (without webpack) for Node.js testing, or consider using mocha-webpack (alternative).","message":"v5.0.0 drops support for the 'node' webpack target; only works in browser-like environments","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Upgrade Node.js to >=10.13 and Mocha to ^5.0.0 or later.","message":"v4.0.0 drops support for Node.js <10.13 and Mocha <5","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Consider using alternatives like karma-mocha-webpack or switch to a dedicated test runner (Jest, Vitest).","message":"The package has not been updated since October 2020. Some users report compatibility issues with newer Mocha versions (>8).","severity":"deprecated","affected_versions":">=5.1.5"},{"fix":"Ensure test code is browser-compatible or use a Node.js target alternative if needed.","message":"This loader runs tests in a browser context (simulated by webpack). Asynchronous operations like timers may behave differently than in Node.js.","severity":"gotcha","affected_versions":"*"},{"fix":"Use webpack config rules as shown in the quickstart.","message":"Inline loader syntax (import 'mocha-loader!./test.js') does not work with webpack's 'entry' field; you must use a configuration with rules.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install --save-dev mocha-loader mocha webpack` and ensure webpack.config.js has `resolveLoader` appropriately (usually defaults).","cause":"Missing installation of mocha-loader or incorrect webpack configuration (not resolving node_modules).","error":"Error: Can't resolve 'mocha-loader'"},{"fix":"Downgrade mocha to v8: `npm install --save-dev mocha@8` or switch to a loader that supports Mocha 9+.","cause":"Incompatible Mocha version (v9+ changed export structure). mocha-loader v5.1.5 only supports mocha up to v8.","error":"TypeError: mocha.run is not a function"},{"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).","cause":"Obsolete dependency issue (mocha-loader v5.1.5 requires css-loader which is missing if not installed).","error":"Cannot find module 'css-loader'"},{"fix":"Remove `target: 'node'` from webpack config, or switch to a different testing setup for Node.js tests (e.g., run mocha directly).","cause":"Using mocha-loader v5+ with webpack target set to 'node'.","error":"Error: webpack target 'node' is not supported by mocha-loader"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}