Jasmine Spec Reporter
jasmine-spec-reporter is a console reporter for the Jasmine behavior-driven development framework, providing real-time output of test results directly in the terminal. It is currently stable at version 7.0.0. The latest major release (v7.0.0) was published in April 2021, indicating a slower release cadence in recent years. The library is designed to offer a highly customizable and readable test reporting experience, especially beneficial in Node.js and Protractor test environments, and ships with comprehensive TypeScript type definitions. Key differentiators include extensive configuration options for output formatting, such as support for different stacktrace displays (none, raw, pretty) and customizable color themes for various test states. It offers a more detailed and configurable spec-by-spec breakdown compared to Jasmine's default reporter.
Common errors
-
TS2345: Argument of type 'SpecReporter' is not assignable to parameter of type 'CustomReporter'.
cause Type mismatch between `jasmine-spec-reporter`'s `SpecReporter` and `@types/jasmine`'s `CustomReporter` interface, often due to differing versions.fixEnsure compatible versions of `jasmine-spec-reporter` and `@types/jasmine`. A temporary workaround might be to use a type assertion: `jasmine.env.addReporter(new SpecReporter(...) as any);` or `as jasmine.CustomReporter;`. -
Property 'successful' does not exist on type 'String'. Did you mean 'toLowerCase'?
cause Attempting to use deprecated String prototype extensions for applying colors within custom display processors after upgrading to v6.0.0 or higher.fixModify your custom `DisplayProcessor` to use the injected `this.theme` object for colorization, e.g., `return this.theme.successful('OK ') + log;`. -
TypeError: Cannot read properties of undefined (reading 'PRETTY')
cause Using `StacktraceOption.PRETTY` (or `RAW`, `NONE`) without importing `StacktraceOption` from `jasmine-spec-reporter`.fixAdd `import { StacktraceOption } from 'jasmine-spec-reporter';` to your file where `SpecReporter` is configured. -
Error: 'displayStacktrace' must be one of 'none', 'raw', 'pretty'
cause Configuring `displayStacktrace` with a boolean value (e.g., `true`) instead of a valid string literal or `StacktraceOption` enum after upgrading to v5.0.0 or higher.fixChange the `displayStacktrace` option in your `SpecReporter` configuration to one of the allowed string values ('none', 'raw', 'pretty') or use the `StacktraceOption` enum (e.g., `displayStacktrace: StacktraceOption.PRETTY`).
Warnings
- breaking The `CustomReporterResult` interface signature was updated to fix collisions with new Jasmine properties. Specifically, `duration` property moved from the top level to `_jsr?: { formattedDuration?: string }`.
- breaking String prototype extensions for colors (e.g., `"OK ".successful`) are no longer supported. Colors must now be applied via the `theme` component available in custom display processors.
- breaking The `displayStacktrace` option in the reporter configuration changed from a boolean to an enum (`StacktraceOption.NONE`, `StacktraceOption.RAW`, `StacktraceOption.PRETTY`).
- breaking The `DisplayProcessor` methods signature in TypeScript changed from using `String` wrapper objects to `string` primitive types for log parameters and return values.
- gotcha The `jasmine-spec-reporter` package needs to be explicitly added to Jasmine's environment via `jasmine.env.addReporter()`. It is not automatically loaded or added to the global `jasmine` object in newer versions.
Install
-
npm install jasmine-spec-reporter -
yarn add jasmine-spec-reporter -
pnpm add jasmine-spec-reporter
Imports
- SpecReporter
const SpecReporter = require('jasmine-spec-reporter')import { SpecReporter } from 'jasmine-spec-reporter' - StacktraceOption
import { StacktraceOption } from 'jasmine-spec-reporter' - DisplayProcessor
import { DisplayProcessor } from 'jasmine-spec-reporter' - CustomReporterResult
import { CustomReporterResult } from 'jasmine-spec-reporter'
Quickstart
import { SpecReporter, StacktraceOption } from 'jasmine-spec-reporter';
import * as Jasmine from 'jasmine'; // Ensure 'jasmine' package is installed
const runner = new Jasmine();
// Clear any default reporters to avoid duplicate output
runner.env.clearReporters();
// Add the SpecReporter with custom configuration
runner.env.addReporter(new SpecReporter({
spec: {
displayStacktrace: StacktraceOption.PRETTY, // Options: 'pretty', 'raw', 'none'
displayDuration: true,
},
suite: {
displayNumber: true,
},
summary: {
displayPending: false,
displayFailed: true,
displayDuration: true,
},
colors: {
success: 'green',
failure: 'red',
pending: 'yellow',
},
// For advanced customization, you can add custom processors:
// customProcessors: [new MyCustomDisplayProcessor()],
}));
// Define a simple test suite for demonstration purposes
runner.describe('A basic test suite', () => {
runner.it('should pass an assertion', () => {
runner.expect(true).toBe(true);
});
runner.it('should fail an assertion', () => {
runner.expect(1).toBe(2);
});
runner.pending('should be a pending test that is not run');
});
// Execute the tests (in a real scenario, this might be handled by a test runner)
runner.execute();