{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mochapack"],"cli":{"name":"mochapack","version":null}},"imports":["npm install --save-dev mocha webpack mochapack\n// package.json scripts section:\n\"test\": \"mochapack \\\"test/**/*.spec.ts\\\"\"","mochapack --webpack-config ./webpack.config.test.js \"test/**/*.js\"","mochapack --watch \"test/**/*.spec.ts\""],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}