Karma Adapter for RequireJS

1.1.0 · abandoned · verified Sun Apr 19

karma-requirejs is a Karma plugin that serves as an adapter, enabling the Karma test runner to execute tests within an environment that utilizes RequireJS for asynchronous module loading. It allows developers to define and load JavaScript modules using the AMD specification during their testing cycles. The package, currently at version 1.1.0, has not seen updates since its last publish date in September 2016, aligning with the broader decline in RequireJS usage in modern web development, which has largely shifted towards ES6 modules and bundlers like Webpack. Its primary function is to bridge Karma's test execution with RequireJS's module resolution, requiring specific configuration in `karma.conf.js` and a separate `test-main.js` file to correctly map module paths and bootstrap tests. This setup is crucial for managing dependencies and ensuring that test files and application code are loaded correctly within the Karma server's `/base` directory context. The dwindling relevance of RequireJS, coupled with the official deprecation of Karma itself, positions karma-requirejs as an unmaintained and largely obsolete tool for new projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core configuration for Karma with RequireJS. It includes a `karma.conf.js` that sets up the RequireJS framework and correctly identifies files to be loaded via RequireJS, along with a `test-main.js` file that configures RequireJS module paths and then dynamically loads and starts the tests.

/* karma.conf.js */
module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      // All application and test files need to be available but NOT included as script tags
      // except for test-main.js which bootstraps RequireJS
      {pattern: 'lib/**/*.js', included: false},
      {pattern: 'src/**/*.js', included: false},
      {pattern: 'test/**/*Spec.js', included: false},
      // test-main.js must be the last file and included normally
      'test/test-main.js'
    ],
    exclude: [
      'src/main.js' // Exclude the main application entry point if it starts the app automatically
    ],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  });
};

/* test/test-main.js */
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
    // then do not normalize the paths.
    allTestFiles.push(file.replace(/\/base\//, '').replace(/\.js$/, ''));
  }
});

require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base/src',

  paths: {
    // Define your module paths here, e.g., for libraries or common components
    'jquery': '../lib/jquery',
    'underscore': '../lib/underscore'
  },

  shim: {
    'underscore': {
      exports: '_'
    },
    'jquery': {
      exports: '$'
    }
  },

  // Dynamically load all test files
  deps: allTestFiles,

  // We have to kickoff Karma, as it is asynchronous
  callback: window.__karma__.start
});

view raw JSON →