Karma jQuery Adapter
karma-jquery is a plugin that provides an adapter for integrating the jQuery framework into the Karma test runner environment. It allows tests executed by Karma to have jQuery available globally, facilitating the testing of jQuery-dependent code. The package is extremely old, with its latest version 0.2.4 published 9 years ago, indicating it is no longer actively maintained. This status is further compounded by the official deprecation of the Karma test runner itself as of July 2024, with no new features or general bug fixes planned. While Karma still receives critical security fixes for a limited time, `karma-jquery` is effectively abandoned. Users should consider modern alternatives for JavaScript testing, such as Jest or Web Test Runner, especially since Angular is providing migration paths off Karma.
Common errors
-
ReferenceError: $ is not defined
cause jQuery is not properly loaded or activated in your Karma tests.fixEnsure `karma-jquery` is installed (`npm install --save-dev karma-jquery`), added to the `plugins` array, and `'jquery'` is included in the `frameworks` array in your `karma.conf.js` file (e.g., `frameworks: ['jasmine', 'jquery']`). Also, confirm the order of frameworks is correct. -
Error: No provider for "framework:jquery"! (Resolving: framework:jquery)
cause The `karma-jquery` plugin has not been registered correctly with Karma.fixVerify that `karma-jquery` is listed in the `plugins` array in `karma.conf.js` (e.g., `plugins: ['karma-jquery', 'karma-*']`). If `karma-*` autodiscovery is not used, `karma-jquery` must be explicitly listed.
Warnings
- breaking The Karma test runner itself has been officially deprecated since July 2024. It is no longer accepting new features or general bug fixes. While critical security issues are still triaged, users are strongly encouraged to migrate to alternative testing solutions.
- deprecated The `karma-jquery` package is unmaintained, with its last release (v0.2.4) over 9 years ago. It may have compatibility issues with newer Node.js versions, browser versions, or even recent versions of Karma before its deprecation.
- gotcha Loading order of frameworks in `karma.conf.js` is crucial. If `jquery` is used with another framework (like `jasmine`), `jquery` should typically be listed after the main testing framework in the `frameworks` array to ensure proper initialization.
Install
-
npm install karma-jquery -
yarn add karma-jquery -
pnpm add karma-jquery
Imports
- karma-jquery plugin registration
import 'karma-jquery'
plugins: ['karma-jquery', ...]
- jQuery framework activation
frameworks: ['karma-jquery']
frameworks: ['jquery', ...]
Quickstart
/* karma.conf.js */
module.exports = function(config) {
config.set({
// Base path that will be used to resolve all relative paths
basePath: '',
// Frameworks to use
// Available frameworks: https://npmjs.org/browse/keyword/karma-framework
// 'jquery' MUST be listed AFTER 'jasmine' or 'mocha' if those are used,
// as the plugin registers the 'jquery' framework.
frameworks: ['jasmine', 'jquery'],
// List of files / patterns to load in the browser
files: [
// Assuming your tests rely on jQuery
'path/to/your/jquery-dependent-code.js',
'path/to/your/tests.js'
],
// List of files / patterns to exclude
exclude: [],
// Preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},
// Test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// Web server port
port: 9876,
// Enable / disable colors in the output (reporters and logs)
colors: true,
// Level of logging.
// Possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// Enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// Start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// If true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// How many browser should be started simultaneously
concurrency: Infinity
});
};