Registry / testing / karma-commonjs

karma-commonjs

JSON →
library1.0.0jsnpmunverified

The `karma-commonjs` plugin facilitates the testing of JavaScript modules written in the CommonJS format within a Karma test runner environment. Rather than bundling, it applies a lightweight wrapper to individual modules, allowing them to interpret `require()` calls directly in the browser. The current stable version, 1.0.0, was last updated in 2016, making it effectively abandoned, especially given that its primary peer dependency, Karma, has also been officially deprecated since 2024. While `karma-commonjs` boasts potentially faster reloads for individual file changes and provides cleaner stack traces by avoiding full bundling, it requires developers to manually list all module files in the Karma configuration. This contrasts with more modern alternatives like `karma-browserify`, which leverage bundlers to automatically discover and include dependencies while also supporting the full Browserify API for transforms and Node.js shims.

npm install karma-commonjs
INSTALL
IMPORT
SIG · KARMA-COMMONJS
K
karma-commonjs
testingjavascriptv1.0.0
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.

commonjs
// karma.conf.js module.exports = function(config) { config.set({ frameworks: ['jasmine', 'commonjs'], // ... }); };
import { commonjs } from 'karma-commonjs';
This package is a Karma plugin and is configured within `karma.conf.js` using string identifiers, not directly imported into user code. 'commonjs' is added to the 'frameworks' array.
commonjs preprocessor
// karma.conf.js module.exports = function(config) { config.set({ // ... preprocessors: { '**/*.js': ['commonjs'] } }); };
const commonjsPreprocessor = require('karma-commonjs');
The 'commonjs' string is used as a preprocessor name in `karma.conf.js` to process files as CommonJS modules.
commonjsPreprocessor.modulesRoot
// karma.conf.js module.exports = function(config) { config.set({ // ... commonjsPreprocessor: { modulesRoot: 'src/modules' } }); };
config.commonjs.modulesRoot = 'src';
Configuration options for the CommonJS preprocessor are nested under `commonjsPreprocessor` in `karma.conf.js`. The `modulesRoot` specifies where `require` calls should look for modules, defaulting to `karma.basePath/node_modules`.

This configuration demonstrates how to set up Karma with `karma-commonjs` to test CommonJS modules. It includes the `commonjs` framework and preprocessor, along with an example of configuring `modulesRoot`.

const path = require('path'); module.exports = function(config) { config.set({ basePath: '', frameworks: ['jasmine', 'commonjs'], files: [ // Your source files (e.g., 'src/**/*.js') 'test/lib.js', 'test/**/*.spec.js' ], exclude: [], preprocessors: { 'test/lib.js': ['commonjs'], 'test/**/*.spec.js': ['commonjs'] }, reporters: ['progress'], port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['ChromeHeadless'], singleRun: false, concurrency: Infinity, // Optional configuration for commonjsPreprocessor commonjsPreprocessor: { modulesRoot: path.resolve(__dirname, 'src') // Example: look for required modules in 'src' folder } }); }; // Example test/lib.js: // module.exports = { greet: (name) => `Hello, ${name}!` }; // Example test/lib.spec.js: // const { greet } = require('./lib'); // describe('greet', () => { // it('should return a greeting', () => { // expect(greet('World')).toBe('Hello, World!'); // }); // });
Debug
Known issues
breakingThe underlying Karma test runner is officially deprecated. `karma-commonjs` is not actively maintained and is unlikely to receive updates or security fixes.
fix
Consider migrating to modern test runners and module bundlers/transpilers (e.g., Jest, Vitest, Web Test Runner with Rollup/Webpack) which offer native ESM/CJS support and better performance.
affects: >=1.0.0
gotchaUnlike bundler-based solutions (e.g., `karma-browserify`), `karma-commonjs` requires you to explicitly specify ALL CommonJS module files in the `files` array and `preprocessors` configuration within `karma.conf.js`. It does not automatically discover `require()`d dependencies.
fix
Ensure every file that acts as a CommonJS module (source or test) is listed in the `files` array and associated with the `commonjs` preprocessor. Failing to do so will result in `require` not being defined for undeclared modules.
affects: >=0.1.0
gotcha`karma-commonjs` provides a simpler CommonJS wrapping mechanism but lacks the full feature set of a bundler like Browserify. It does not natively support Browserify transforms, plugins, or shims for Node.js core modules.
fix
If your project relies on advanced Browserify features, consider using `karma-browserify` instead. For simple CommonJS module testing without complex bundling needs, `karma-commonjs` might suffice but be aware of its limitations.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: require is not defined
A file attempting to use `require()` was not processed by the `commonjs` preprocessor, or a required module was not included in Karma's `files` configuration.
fix
Ensure the file (and its dependencies) are listed in `karma.conf.js` within the `files` array and have the `commonjs` preprocessor applied: `preprocessors: { 'path/to/my-module.js': ['commonjs'] }`.
Error: Cannot find module 'some-module'
The module being `require()`d is not found at the expected path, or the `commonjsPreprocessor.modulesRoot` is incorrectly configured.
fix
Verify the module's path is correct relative to the file requiring it, or adjust the `commonjsPreprocessor.modulesRoot` option in `karma.conf.js` to point to the directory containing your modules.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
karmarequiredCore test runner that this plugin extends.
Agent activity
2 hits · last 30 days
node
2
Resources
karma-commonjs — npm install karma-commonjs · libregistry