{"id":11203,"library":"karma-requirejs","title":"Karma Adapter for RequireJS","description":"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.","status":"abandoned","version":"1.1.0","language":"javascript","source_language":"en","source_url":"git://github.com/karma-runner/karma-requirejs","tags":["javascript","karma-plugin","karma-adapter","requirejs"],"install":[{"cmd":"npm install karma-requirejs","lang":"bash","label":"npm"},{"cmd":"yarn add karma-requirejs","lang":"bash","label":"yarn"},{"cmd":"pnpm add karma-requirejs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Runtime peer dependency for Karma test runner integration.","package":"karma","optional":false},{"reason":"Runtime peer dependency for AMD module loading.","package":"requirejs","optional":false}],"imports":[{"note":"The 'requirejs' string is added to the frameworks array in `karma.conf.js` to enable the plugin.","symbol":"frameworks","correct":"module.exports = function(config) { config.set({ frameworks: ['jasmine', 'requirejs'] }); };"},{"note":"RequireJS-managed files must have `included: false` in `karma.conf.js` to prevent Karma from loading them directly via script tags. `test-main.js` must be the last entry and `included: true` (default).","wrong":"files: ['src/**/*.js', 'test/test-main.js']","symbol":"files","correct":"files: [{pattern: 'src/**/*.js', included: false}, 'test/test-main.js']"},{"note":"This configuration is placed in a separate `test-main.js` file, defining RequireJS's `baseUrl` (relative to Karma's `/base` directory), listing test dependencies, and initiating Karma's test run.","symbol":"require.config","correct":"require.config({ baseUrl: '/base/src', deps: allTestFiles, callback: window.__karma__.start });"}],"quickstart":{"code":"/* karma.conf.js */\nmodule.exports = function(config) {\n  config.set({\n    basePath: '',\n    frameworks: ['jasmine', 'requirejs'],\n    files: [\n      // All application and test files need to be available but NOT included as script tags\n      // except for test-main.js which bootstraps RequireJS\n      {pattern: 'lib/**/*.js', included: false},\n      {pattern: 'src/**/*.js', included: false},\n      {pattern: 'test/**/*Spec.js', included: false},\n      // test-main.js must be the last file and included normally\n      'test/test-main.js'\n    ],\n    exclude: [\n      'src/main.js' // Exclude the main application entry point if it starts the app automatically\n    ],\n    preprocessors: {},\n    reporters: ['progress'],\n    port: 9876,\n    colors: true,\n    logLevel: config.LOG_INFO,\n    autoWatch: true,\n    browsers: ['Chrome'],\n    singleRun: false,\n    concurrency: Infinity\n  });\n};\n\n/* test/test-main.js */\nvar allTestFiles = [];\nvar TEST_REGEXP = /(spec|test)\\.js$/i;\n\n// Get a list of all the test files to include\nObject.keys(window.__karma__.files).forEach(function(file) {\n  if (TEST_REGEXP.test(file)) {\n    // Normalize paths to RequireJS module names.\n    // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)\n    // then do not normalize the paths.\n    allTestFiles.push(file.replace(/\\/base\\//, '').replace(/\\.js$/, ''));\n  }\n});\n\nrequire.config({\n  // Karma serves files under /base, which is the basePath from your config file\n  baseUrl: '/base/src',\n\n  paths: {\n    // Define your module paths here, e.g., for libraries or common components\n    'jquery': '../lib/jquery',\n    'underscore': '../lib/underscore'\n  },\n\n  shim: {\n    'underscore': {\n      exports: '_'\n    },\n    'jquery': {\n      exports: '$'\n    }\n  },\n\n  // Dynamically load all test files\n  deps: allTestFiles,\n\n  // We have to kickoff Karma, as it is asynchronous\n  callback: window.__karma__.start\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"For new projects, consider modern alternatives like Web Test Runner, Jest, or Vitest. For existing projects, evaluate migrating away from RequireJS and Karma.","message":"The underlying Karma test runner is officially deprecated by the Angular team, and RequireJS itself is largely superseded by ES6 modules and modern bundlers (e.g., Webpack, Vite). This `karma-requirejs` package is unmaintained, meaning no new features, bug fixes, or compatibility updates will be provided.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure all RequireJS-managed files use `{pattern: 'path/**/*.js', included: false}`. The `test-main.js` file should be listed last, e.g., `'test/test-main.js'`.","message":"Correctly configuring the `files` array in `karma.conf.js` is critical. Application and test files intended for RequireJS loading must be listed with `included: false`. Only the `test-main.js` file (which bootstraps RequireJS) should be included directly and must be the last entry.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set `baseUrl` in `test-main.js` to start with `/base/`, e.g., `baseUrl: '/base/src'`. Double-check all module paths defined in `require.config` relative to this `baseUrl`.","message":"RequireJS's `baseUrl` in `test-main.js` must accurately map to the Karma server's `/base` directory. Incorrect relative paths or `baseUrl` settings will result in 'Script error' messages, as RequireJS won't find your modules.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Test thoroughly in your target environments. If encountering unexpected errors, consider pinning older Node.js versions or replacing `karma-requirejs` with a more modern testing setup.","message":"Being last updated in 2016, this plugin may have compatibility issues with newer versions of Node.js, modern browser environments, or other Karma plugins that rely on more recent JavaScript features or promise implementations.","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":"Verify `baseUrl` in `test-main.js` and `paths` configuration. Ensure the module's file pattern is listed in `karma.conf.js` `files` array with `included: false`, and that `test-main.js` is included last. Check browser console for network errors (404s).","cause":"RequireJS failed to load a module, often due to an incorrect path in `require.config` or the file not being served by Karma.","error":"Uncaught Error: Script error for: [module name]"},{"fix":"Ensure `test-main.js` is the last entry in `karma.conf.js` `files` array (and *not* `included: false`). Verify `test-main.js`'s `deps` array correctly identifies your spec files (e.g., `allTestFiles`) and that `window.__karma__.start` is called in the `callback`.","cause":"Karma is running but not finding any tests, typically because RequireJS isn't correctly bootstrapping the tests or test files aren't being loaded.","error":"Executed 0 of 0 ERROR"},{"fix":"Double-check `basePath` in `karma.conf.js` and ensure that file patterns like `{pattern: 'src/**/*.js', included: false}` correctly resolve to existing files. Review the `baseUrl` in `test-main.js` to ensure it matches the server's path (e.g., starting with `/base/`). Karma serves files under `/base`.","cause":"Karma internal error related to serving files, often when `basePath` or file patterns are misconfigured relative to the project root and `test-main.js`.","error":"There is no timestamp for /base/src/app.js!"}],"ecosystem":"npm"}