{"id":13415,"library":"karma-mocha","title":"Karma Mocha Adapter","description":"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.","status":"maintenance","version":"2.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/karma-runner/karma-mocha","tags":["javascript","karma-plugin","karma-adapter","mocha"],"install":[{"cmd":"npm install karma-mocha","lang":"bash","label":"npm"},{"cmd":"yarn add karma-mocha","lang":"bash","label":"yarn"},{"cmd":"pnpm add karma-mocha","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as the test runner framework for which karma-mocha acts as an adapter.","package":"karma","optional":false},{"reason":"The underlying testing framework that karma-mocha integrates with Karma.","package":"mocha","optional":false}],"imports":[{"note":"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`.","wrong":"import 'karma-mocha';","symbol":"frameworks","correct":"frameworks: ['mocha']"},{"note":"Mocha-specific configurations (like reporters, UI, or `grep` patterns) are passed via the `client.mocha` object in `karma.conf.js`.","symbol":"client.mocha","correct":"client: { mocha: { reporter: 'html' } }"},{"note":"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`.","symbol":"Mocha.opts","correct":"client: { mocha: { opts: 'test/mocha.opts' } }"}],"quickstart":{"code":"/* karma.conf.js */\nmodule.exports = function(config) {\n  config.set({\n    frameworks: ['mocha'],\n    files: [\n      'test/**/*.js' // Ensure your test files are included\n    ],\n    browsers: ['ChromeHeadless'], // Example: run tests in headless Chrome\n    reporters: ['progress'], // Example: show progress in console\n    client: {\n      mocha: {\n        ui: 'bdd', // Use Mocha's BDD interface\n        timeout: 2000, // Set test timeout\n        grep: process.env.MOCHA_GREP ?? '' // Pass grep pattern from env var\n      }\n    },\n    // Optional: watch for file changes and re-run tests\n    autoWatch: true\n  });\n};\n\n/* test/example.js */\nconst assert = require('assert');\n\ndescribe('Array', function() {\n  describe('#indexOf()', function() {\n    it('should return -1 when the value is not present', function() {\n      assert.strictEqual([1, 2, 3].indexOf(4), -1);\n    });\n\n    it('should return the correct index when the value is present', function() {\n      assert.strictEqual([1, 2, 3].indexOf(2), 1);\n    });\n  });\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Upgrade to a supported Node.js version, preferably Node.js 10 or higher for compatibility with recent Karma versions.","message":"Version 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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"Passing 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always install `karma`, `mocha`, and `karma-mocha` as dev dependencies: `npm install --save-dev karma mocha karma-mocha`.","message":"karma-mocha requires both `karma` and `mocha` packages to be installed alongside it. Forgetting one will lead to a non-functional setup.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Install mocha as a dev dependency: `npm install mocha --save-dev`.","cause":"Mocha is not installed or not resolvable in the project's node_modules.","error":"Cannot find module 'mocha' from 'C:\\path\\to\\your\\project'"},{"fix":"Ensure `karma-mocha` is installed (`npm install karma-mocha --save-dev`) and 'mocha' is present in the `frameworks` array in `karma.conf.js`.","cause":"karma-mocha plugin is not installed or not correctly listed in Karma's `frameworks` array.","error":"Error: There is no adapter for the \"mocha\" framework."},{"fix":"Verify that `frameworks: ['mocha']` is correctly set and that `client.mocha` options are nested correctly within the `client` object in `karma.conf.js`.","cause":"Attempting to configure Mocha-specific options under `client.mocha` when `mocha` is not specified as a framework, or `client` object is malformed.","error":"TypeError: Cannot read properties of undefined (reading 'ui') or similar errors related to client.mocha properties."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}