Karma CommonJS Plugin

1.0.0 · abandoned · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

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!');
//   });
// });

view raw JSON →