Registry / testing / karma-mocha

karma-mocha

JSON →
library2.0.1jsnpmunverified

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-mocha
INSTALL
IMPORT
SIG · KARMA-MOCHA
K
karma-mocha
testingjavascriptv2.0.1
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.

frameworks
frameworks: ['mocha']
import 'karma-mocha';
karma-mocha is a Karma plugin, not imported via ES modules or CommonJS. It's configured by specifying 'mocha' in Karma's `frameworks` array within `karma.conf.js`.
client.mocha
client: { mocha: { reporter: 'html' } }
Mocha-specific configurations (like reporters, UI, or `grep` patterns) are passed via the `client.mocha` object in `karma.conf.js`.
Mocha.opts
client: { mocha: { opts: 'test/mocha.opts' } }
Existing Mocha configuration files (`mocha.opts`) can be loaded by specifying their path via `client.mocha.opts`. Setting `opts: true` will load from the default location `test/mocha.opts`.

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.

/* karma.conf.js */ module.exports = function(config) { config.set({ frameworks: ['mocha'], files: [ 'test/**/*.js' // Ensure your test files are included ], browsers: ['ChromeHeadless'], // Example: run tests in headless Chrome reporters: ['progress'], // Example: show progress in console client: { mocha: { ui: 'bdd', // Use Mocha's BDD interface timeout: 2000, // Set test timeout grep: process.env.MOCHA_GREP ?? '' // Pass grep pattern from env var } }, // Optional: watch for file changes and re-run tests autoWatch: true }); }; /* test/example.js */ const assert = require('assert'); describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { assert.strictEqual([1, 2, 3].indexOf(4), -1); }); it('should return the correct index when the value is present', function() { assert.strictEqual([1, 2, 3].indexOf(2), 1); }); }); });
Debug
Known issues
breakingVersion 2.0.0 updated Node.js versions, potentially dropping support for older Node.js runtimes. Users on older Node.js environments may encounter build or runtime issues.
fix
Upgrade to a supported Node.js version, preferably Node.js 10 or higher for compatibility with recent Karma versions.
affects: >=2.0.0
gotchaPassing Mocha options via `client.mocha` in `karma.conf.js` overrides any options set directly in a `mocha.opts` file unless explicitly loaded. Be aware of the precedence.
fix
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.
affects: >=1.0.0
gotchakarma-mocha requires both `karma` and `mocha` packages to be installed alongside it. Forgetting one will lead to a non-functional setup.
fix
Always install `karma`, `mocha`, and `karma-mocha` as dev dependencies: `npm install --save-dev karma mocha karma-mocha`.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'mocha' from 'C:\path\to\your\project'
Mocha is not installed or not resolvable in the project's node_modules.
fix
Install mocha as a dev dependency: `npm install mocha --save-dev`.
Error: There is no adapter for the "mocha" framework.
karma-mocha plugin is not installed or not correctly listed in Karma's `frameworks` array.
fix
Ensure `karma-mocha` is installed (`npm install karma-mocha --save-dev`) and 'mocha' is present in the `frameworks` array in `karma.conf.js`.
TypeError: Cannot read properties of undefined (reading 'ui') or similar errors related to client.mocha properties.
Attempting to configure Mocha-specific options under `client.mocha` when `mocha` is not specified as a framework, or `client` object is malformed.
fix
Verify that `frameworks: ['mocha']` is correctly set and that `client.mocha` options are nested correctly within the `client` object in `karma.conf.js`.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
karmarequiredRequired as the test runner framework for which karma-mocha acts as an adapter.
mocharequiredThe underlying testing framework that karma-mocha integrates with Karma.
Agent activity
2 hits · last 30 days
node
2
Resources
karma-mocha — npm install karma-mocha · libregistry