karma-mocha is an adapter plugin that integrates the Mocha testing framework with Karma, a test runner for JavaScript. It allows developers to run their Mocha-based tests in various browsers and environments through Karma's powerful test execution capabilities. The current stable version is 2.0.1, released in April 2020. While the package has not seen frequent updates since then, its core functionality remains stable and widely used within the Karma ecosystem. Key differentiators include its seamless integration with Karma's configuration, enabling browser-based testing for Mocha tests, and options for passing specific Mocha CLI arguments (like `grep` or `reporter`) directly through Karma's client-side configuration. It abstracts away the complexities of running Mocha tests across different browsers, providing a consistent testing environment.
npm install karma-mochaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a minimal Karma configuration (`karma.conf.js`) to run Mocha tests in a headless browser, along with a simple Mocha test file (`test/example.js`). It shows how to include test files, specify the Mocha framework, and configure basic Mocha options via Karma's `client` settings.
Upgrade to a supported Node.js version, preferably Node.js 10 or higher for compatibility with recent Karma versions.
If using a `mocha.opts` file, ensure you either load it explicitly with `client.mocha.opts: 'path/to/mocha.opts'` or verify that your `client.mocha` configuration doesn't conflict with or unintentionally replace crucial settings from your `.opts` file.
Always install `karma`, `mocha`, and `karma-mocha` as dev dependencies: `npm install --save-dev karma mocha karma-mocha`.
Install mocha as a dev dependency: `npm install mocha --save-dev`.
Ensure `karma-mocha` is installed (`npm install karma-mocha --save-dev`) and 'mocha' is present in the `frameworks` array in `karma.conf.js`.
Verify that `frameworks: ['mocha']` is correctly set and that `client.mocha` options are nested correctly within the `client` object in `karma.conf.js`.