{"id":17035,"library":"mochapack","title":"Mochapack: Webpack-integrated Mocha Test Runner","description":"Mochapack is a command-line utility designed to seamlessly integrate Webpack's powerful asset compilation and module resolution directly into the Mocha test runner. Forked from the unmaintained `mocha-webpack`, it provides an optimized testing experience by precompiling test files with Webpack, automatically handling source maps, and avoiding temporary file writes to disk. A key differentiator is its 'watch' mode, which intelligently re-runs only the test files affected by a change, leveraging Webpack's dependency graph for efficient feedback during development. It aims to offer a CLI experience nearly identical to Mocha's, while enabling full Webpack benefits like custom path resolution within tests. The current stable version is 2.1.5, with releases typically occurring on a quarterly basis, focusing on dependency updates, Webpack/Mocha compatibility, and bug fixes.","status":"active","version":"2.1.5","language":"javascript","source_language":"en","source_url":"https://github.com/sysgears/mochapack","tags":["javascript","webpack","mocha","typescript"],"install":[{"cmd":"npm install mochapack","lang":"bash","label":"npm"},{"cmd":"yarn add mochapack","lang":"bash","label":"yarn"},{"cmd":"pnpm add mochapack","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required test runner, supports versions 5.x.x - 9.x.x.","package":"mocha","optional":false},{"reason":"Required for test precompilation, supports versions 4.x.x - 5.x.x.","package":"webpack","optional":false}],"imports":[{"note":"Mochapack is a CLI tool and does not export JavaScript/TypeScript symbols for direct programmatic import. It is primarily used via npm scripts or direct command-line invocation.","wrong":"import mochapack from 'mochapack';","symbol":"mochapack CLI execution","correct":"npm install --save-dev mocha webpack mochapack\n// package.json scripts section:\n\"test\": \"mochapack \\\"test/**/*.spec.ts\\\"\""},{"note":"Mochapack can automatically detect 'webpack.config.js' but explicitly specifying the config file path with `--webpack-config` is recommended for clarity, especially for test-specific configurations. Webpack configurations often need `target: 'node'` and `externals` to avoid bundling node modules.","wrong":"mochapack --webpack-config webpack.config.js","symbol":"Webpack configuration integration","correct":"mochapack --webpack-config ./webpack.config.test.js \"test/**/*.js\""},{"note":"The `--watch` flag enables Mochapack's intelligent watch mode, which unlike Mocha's, re-runs only affected tests based on the dependency graph, offering faster feedback.","wrong":"mocha --watch \"test/**/*.spec.ts\"","symbol":"Watch mode activation","correct":"mochapack --watch \"test/**/*.spec.ts\""}],"quickstart":{"code":"// First, install dependencies:\n// npm install --save-dev mocha webpack mochapack ts-node chai\n\n// package.json (excerpt)\n// {\n//   \"devDependencies\": {\n//     \"mocha\": \"^9.0.0\",\n//     \"webpack\": \"^5.0.0\",\n//     \"mochapack\": \"^2.0.0\",\n//     \"ts-node\": \"^10.0.0\",\n//     \"chai\": \"^4.0.0\"\n//   },\n//   \"scripts\": {\n//     \"test\": \"mochapack --require ts-node/register \\\"test/**/*.spec.ts\\\"\"\n//   }\n// }\n\n// webpack.config.test.js (simplified example)\nconst nodeExternals = require('webpack-node-externals');\n\nmodule.exports = {\n  mode: 'development',\n  target: 'node',\n  externals: [nodeExternals()],\n  devtool: 'inline-cheap-module-source-map',\n  resolve: {\n    extensions: ['.ts', '.js', '.json']\n  },\n  module: {\n    rules: [\n      {\n        test: /\\.ts$/,\n        loader: 'ts-loader',\n        options: { transpileOnly: true }\n      }\n    ]\n  }\n};\n\n// test/calculator.spec.ts\nimport { expect } from 'chai';\nimport { describe, it } from 'mocha';\n\nclass Calculator {\n  add(a: number, b: number): number { return a + b; }\n  subtract(a: number, b: number): number { return a - b; }\n}\n\ndescribe('Calculator', () => {\n  const calculator = new Calculator();\n\n  it('should correctly add two numbers', () => {\n    expect(calculator.add(2, 3)).to.equal(5);\n  });\n\n  it('should correctly subtract two numbers', () => {\n    expect(calculator.subtract(5, 2)).to.equal(3);\n  });\n});\n\n// To run these tests: npm test","lang":"typescript","description":"This quickstart demonstrates how to set up Mochapack with TypeScript, Webpack, and Mocha. It includes `package.json` scripts, a basic Webpack configuration for testing Node.js environments, and a sample TypeScript test file. The setup shows how to run tests using a glob pattern and register `ts-node` for TypeScript compilation on the fly."},"warnings":[{"fix":"Upgrade Mocha to a compatible version (5.x.x - 9.x.x). Check your `package.json` for `mocha` and update its version.","message":"Mochapack v2.0.0 dropped support for Mocha 4.x. Projects using older Mocha versions must upgrade to Mocha 5.x.x or higher when updating Mochapack to v2.0.0 or later.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use double quotes around glob patterns, e.g., `mochapack \"test/**/*.js\"` instead of `mochapack test/**/*.js`.","message":"When specifying test file glob patterns directly in the command line or `package.json` scripts, always enclose them in double quotes. Most terminals resolve glob patterns automatically before passing them to the command, leading to incorrect file matching if unquoted.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Verify that your installed versions of `mocha` and `webpack` meet Mochapack's peer dependency requirements by checking your `package.json` and `node_modules`.","message":"Mochapack relies on `mocha` and `webpack` as peer dependencies. Ensure compatible versions are installed in your project. Mochapack 2.x supports Webpack 4.x.x - 5.x.x and Mocha 5.x.x - 9.x.x.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate your `mocha-webpack` setup to `mochapack`. Install `mochapack` and update your `package.json` scripts and configurations accordingly.","message":"Mochapack is a fork of `mocha-webpack`. The original `mocha-webpack` project is no longer actively maintained. Users are encouraged to migrate to `mochapack` for ongoing support, new features, and compatibility with modern Webpack and Mocha versions.","severity":"deprecated","affected_versions":"All versions (for `mocha-webpack` users)"},{"fix":"Ensure you are on Mochapack version `2.0.4` or newer if you rely on the `--file` option for loading setup files. Alternatively, use `--require` to load modules before tests, which is often more robust.","message":"The `--file` option for including pre-test files might not have been respected in early 2.x versions, leading to setup scripts not being executed. This was addressed in later patch releases.","severity":"gotcha","affected_versions":">=2.0.0 <2.0.4"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Install the required peer dependencies: `npm install --save-dev webpack mocha`.","cause":"Mochapack's peer dependencies (webpack and mocha) are not installed or are not resolvable.","error":"Error: Cannot find module 'webpack' or Error: Cannot find module 'mocha'"},{"fix":"Ensure your glob patterns are correctly quoted (e.g., `mochapack \"test/**/*.js\"`). Verify your webpack configuration (especially `resolve.extensions` and `module.rules`) correctly handles your test file types and paths.","cause":"This often occurs when Mochapack cannot find any test files to run. A common reason is incorrect or unquoted glob patterns in the CLI command, or issues with webpack's resolution/transpilation.","error":"0 passing (0ms)"},{"fix":"Review the output preceding this error for specific Webpack compilation messages or Mocha test failure reports. Check your webpack configuration for any syntax errors or incompatible loaders.","cause":"A generic error indicating that Mochapack encountered a problem during execution, which could range from compilation failures (Webpack errors) to test failures (Mocha errors).","error":"ERROR mochapack exited with code 1"},{"fix":"In your `webpack.config.js` used with mochapack, ensure `target: 'node'` is set. Also, consider using `webpack-node-externals` to prevent bundling Node.js native modules.","cause":"This error typically indicates that Webpack is compiling your test code for a browser environment when it should be compiled for Node.js, usually due to a missing or incorrect `target` setting in your webpack configuration.","error":"ReferenceError: window is not defined"}],"ecosystem":"npm","meta_description":null}