A Karma plugin that dynamically displays Jasmine test results at debug.html page
npx @tessl/cli install tessl/npm-karma-jasmine-html-reporter@2.1.0Karma Jasmine HTML Reporter is a Karma plugin that provides a dynamic HTML interface for viewing Jasmine test results. It displays test results at the debug.html page during test execution, allowing developers to run individual tests or entire test suites interactively.
npm install karma-jasmine-html-reporter --save-devThis package is a Karma plugin and is not imported directly into code. Instead, it's loaded via Karma configuration:
// karma.conf.js - require in plugins array
module.exports = function(config) {
config.set({
plugins: [
require('karma-jasmine'),
require('karma-jasmine-html-reporter')
],
reporters: ['kjhtml']
});
};// karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
plugins: [
require('karma-jasmine'),
require('karma-jasmine-html-reporter')
],
reporters: ['kjhtml']
});
};The plugin consists of two main components:
Main Karma plugin export that provides the 'kjhtml' reporter.
/**
* Main plugin export - Karma plugin registration
* Registers the 'kjhtml' reporter with Karma
*/
module.exports = {
'reporter:kjhtml': ['type', initReporter]
};Core function that initializes the HTML reporter and configures file injection.
/**
* Initializes the HTML reporter and sets up file injection
* @param {Object} karmaConfig - Karma configuration object
* @param {Function} baseReporterDecorator - Base reporter decorator from Karma
*/
function initReporter(karmaConfig, baseReporterDecorator) {
// Implementation handles:
// - Base reporter decoration
// - Configuration option processing
// - Jasmine core file injection
// - Boot script injection
}
// Dependency injection specification
initReporter.$inject = ['config', 'baseReporterDecorator'];Internal utility function used by the reporter to create Karma file pattern objects.
/**
* Creates a Karma file pattern object for injecting files
* @param {string} path - The file path to include
* @returns {Object} Karma file pattern object
*/
function createPattern(path) {
return {
pattern: path,
included: true,
served: true,
watched: false
};
}Optional configuration to suppress console output when using multiple reporters.
/**
* Optional configuration object for the HTML reporter
* Set in karma.conf.js under jasmineHtmlReporter key
*/
interface JasmineHtmlReporterConfig {
/** Suppress all console output (overrides other suppress settings) */
suppressAll?: boolean;
/** Suppress failed test console output only */
suppressFailed?: boolean;
}Usage Example:
// karma.conf.js
module.exports = function(config) {
config.set({
reporters: ['kjhtml', 'mocha'], // Multiple reporters
jasmineHtmlReporter: {
suppressAll: true, // Suppress all messages
suppressFailed: true // Suppress failed messages only
}
});
};The boot.js script provides the HTML interface initialization:
/**
* Jasmine HTML interface configuration from URL query parameters
*/
interface RuntimeConfig {
/** Stop execution on first spec failure */
stopOnSpecFailure?: boolean;
/** Stop spec on first expectation failure */
stopSpecOnExpectationFailure?: boolean;
/** Hide disabled specs in the UI */
hideDisabled?: boolean;
/** Random execution seed */
random?: string;
/** Specific seed for random execution */
seed?: string;
}
/**
* Jasmine Environment interface (from jasmine-core)
*/
interface JasmineEnv {
configure(config: any): void;
addReporter(reporter: any): void;
}
/**
* Jasmine Timer interface (from jasmine-core)
*/
interface JasmineTimer {
start(): void;
elapsed(): number;
}
/**
* HTML Reporter instance configuration
*/
interface HtmlReporterOptions {
/** Jasmine environment instance */
env: JasmineEnv;
/** Navigation function for URL parameter changes */
navigateWithNewParam: (key: string, value: string) => void;
/** Query string manipulation function */
addToExistingQueryString: (key: string, value: string) => string;
/** Container element provider */
getContainer: () => HTMLElement;
/** DOM element creation function */
createElement: (...args: any[]) => HTMLElement;
/** Text node creation function */
createTextNode: (...args: any[]) => Text;
/** Timer instance for test execution timing */
timer: JasmineTimer;
/** Whether to enable spec filtering */
filterSpecs: boolean;
}These packages must be installed in the project using karma-jasmine-html-reporter:
{
"jasmine-core": "^4.0.0 || ^5.0.0",
"karma": "^6.0.0",
"karma-jasmine": "^5.0.0"
}The plugin automatically injects these files from jasmine-core:
The HTML interface supports URL query parameters for test execution control:
spec: Filter tests by name patternstopOnSpecFailure: Stop on first test failurestopSpecOnExpectationFailure: Stop on first assertion failurehideDisabled: Hide disabled tests in the UIrandom: Enable random test executionseed: Set specific seed for random executionThe plugin integrates with Karma's reporter system and inherits standard Karma error handling. Configuration errors are handled through Karma's configuration validation system.