Karma Jasmine Adapter
karma-jasmine is a Karma plugin that serves as an adapter for the Jasmine testing framework, enabling developers to run their Jasmine-based unit tests within the Karma test runner environment. The current stable version is 5.1.0, released in June 2022, with a consistent release cadence addressing bug fixes, dependency updates (including `jasmine-core`), and minor feature enhancements. It primarily functions by providing the `jasmine` framework to Karma's configuration, allowing seamless integration and execution of Jasmine specs. Key differentiators include its tight coupling with the Karma ecosystem, offering features like custom spec filtering, debug-by-URL capabilities for focused testing, and basic sharding support. This package is essential for projects using Karma for test automation and Jasmine for writing their tests, abstracting away the complexities of setting up the two together.
Common errors
-
Error: Cannot find module 'karma' from '<path>'
cause Karma is a peer dependency and must be installed separately.fixInstall Karma: `npm install karma --save-dev` -
TypeError: config.set is not a function
cause Attempting to use `require('karma')` directly to configure Karma instead of exporting a function that receives the config object.fixEnsure `karma.conf.js` exports a function: `module.exports = function(config) { /* ... */ }`. -
No specs found
cause The `files` array in `karma.conf.js` does not correctly point to any Jasmine spec files.fixReview the `files` array in `karma.conf.js` and ensure the glob patterns or file paths accurately include your Jasmine test files (e.g., `['spec/**/*.js']`). -
Jasmine is not defined
cause Jasmine environment functions (e.g., `describe`, `it`, `expect`) are globally available in spec files but not necessarily in auxiliary scripts or outside the test execution context.fixEnsure your Jasmine spec files are correctly loaded by Karma-Jasmine and that you are writing test code within `describe` blocks. This error can also occur if `frameworks: ['jasmine']` is missing from `karma.conf.js`.
Warnings
- breaking Version 5.0.0 of karma-jasmine dropped support for Node.js 10. Projects using Node.js 10 or older will need to upgrade their Node.js environment to version 12 or higher to use karma-jasmine v5.
- breaking With the release of v5.0.0, the `karma` peer dependency was strictly limited to `^6.0.0`. Older versions of Karma are no longer supported by `karma-jasmine` v5.
- breaking Version 4.0.0 dropped support for Node.js 8. Projects still on Node.js 8 will encounter issues. This version also added support for Node.js 14.
- gotcha Incorrectly configuring `files` array in `karma.conf.js` can lead to tests not being found or unexpected execution order. Ensure test files and any required source files are correctly included.
Install
-
npm install karma-jasmine -
yarn add karma-jasmine -
pnpm add karma-jasmine
Imports
- frameworks
import 'karma-jasmine';
config.set({ frameworks: ['jasmine'] }) - client.jasmine
config.set({ client: { jasmine: { random: true } } }) - client.args (grep)
karma start --grep=<pattern>
config.set({ client: { args: ['--grep', '<pattern>'] } })
Quickstart
const path = require('path');
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
// Assuming your test files are in a 'spec' directory
path.resolve(__dirname, 'src/**/*.js'),
path.resolve(__dirname, 'spec/**/*.spec.js')
],
browsers: ['ChromeHeadless'],
reporters: ['progress'],
autoWatch: true,
singleRun: false,
client: {
jasmine: {
random: true,
seed: '12345',
oneFailurePerSpec: false,
failFast: false,
timeoutInterval: 5000
},
// Example of custom spec filter, though typically done via `karma run -- --grep` or `jasmine.getEnv().configure`
// args: ['--grep', 'spec that succeeds']
}
});
};