or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-jasmine-spec-reporter

Spec reporter for jasmine behavior-driven development framework

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jasmine-spec-reporter@7.0.x

To install, run

npx @tessl/cli install tessl/npm-jasmine-spec-reporter@7.0.0

index.mddocs/

Jasmine Spec Reporter

Jasmine Spec Reporter is a customizable real-time console spec reporter for the Jasmine testing framework, designed to enhance the visual presentation of test results in command-line interfaces. It offers extensive configuration options for formatting test output, including colored displays, custom themes, execution metrics, and various display processors that can be tailored to different testing environments.

Package Information

  • Package Name: jasmine-spec-reporter
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jasmine-spec-reporter --save-dev

Core Imports

import { SpecReporter, DisplayProcessor, StacktraceOption } from "jasmine-spec-reporter";

For CommonJS:

const { SpecReporter, DisplayProcessor, StacktraceOption } = require("jasmine-spec-reporter");

Note: The parse function from the configuration parser is available internally but not exported in the main module. Configuration parsing is handled automatically when passing a configuration object to the SpecReporter constructor.

Basic Usage

import { SpecReporter } from "jasmine-spec-reporter";

// Clear default reporters and add the spec reporter
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(new SpecReporter({
  spec: {
    displayPending: true,
    displayDuration: true,
    displayStacktrace: "none"
  },
  summary: {
    displayDuration: false,
    displaySuccessful: true
  }
}));

Architecture

Jasmine Spec Reporter is built around several key components:

  • SpecReporter: Main reporter class that implements Jasmine's CustomReporter interface
  • Configuration System: Comprehensive configuration options for customizing output appearance
  • Display Processors: Modular system for customizing how different parts of the output are formatted
  • Theme System: Color and styling management using the colors package
  • Execution Metrics: Performance tracking and timing information

Capabilities

Main Reporter

The primary reporter class that integrates with Jasmine's reporting system.

class SpecReporter implements jasmine.CustomReporter {
  /**
   * Creates a new SpecReporter instance
   * @param configuration - Optional configuration object to customize reporter behavior
   */
  constructor(configuration?: Configuration);
  
  /**
   * Called when Jasmine starts running tests
   * @param suiteInfo - Information about the test suite including total specs defined
   */
  jasmineStarted(suiteInfo: jasmine.SuiteInfo): void;
  
  /**
   * Called when Jasmine finishes running tests
   * @param runDetails - Details about the test run including execution order and global errors
   */
  jasmineDone(runDetails: jasmine.RunDetails): void;
  
  /**
   * Called when a test suite starts
   * @param result - Suite result information
   */
  suiteStarted(result: CustomReporterResult): void;
  
  /**
   * Called when a test suite finishes
   * @param result - Suite result information including any failed expectations
   */
  suiteDone(result: CustomReporterResult): void;
  
  /**
   * Called when an individual spec starts
   * @param result - Spec result information
   */
  specStarted(result: CustomReporterResult): void;
  
  /**
   * Called when an individual spec finishes
   * @param result - Spec result information including status and execution details
   */
  specDone(result: CustomReporterResult): void;
}

Usage Example:

import { SpecReporter } from "jasmine-spec-reporter";

const reporter = new SpecReporter({
  spec: {
    displayErrorMessages: true,
    displayStacktrace: "pretty",
    displaySuccessful: true,
    displayFailed: true,
    displayPending: true,
    displayDuration: true
  },
  colors: {
    enabled: true,
    successful: "green",
    failed: "red",
    pending: "yellow"
  }
});

jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(reporter);

Configuration System

Comprehensive configuration options for customizing the reporter's output appearance and behavior.

/**
 * Parse and merge configuration with default values
 * @param conf - Optional user configuration to merge with defaults
 * @returns Complete configuration object with merged values
 */
function parse(conf?: Configuration): Configuration;

class Configuration {
  suite?: {
    displayNumber?: boolean;
  };
  spec?: {
    displayErrorMessages?: boolean;
    displayStacktrace?: StacktraceOption;
    displaySuccessful?: boolean;
    displayFailed?: boolean;
    displayPending?: boolean;
    displayDuration?: boolean;
  };
  summary?: {
    displayErrorMessages?: boolean;
    displayStacktrace?: StacktraceOption;
    displaySuccessful?: boolean;
    displayFailed?: boolean;
    displayPending?: boolean;
    displayDuration?: boolean;
  };
  colors?: {
    enabled?: boolean;
    successful?: string;
    failed?: string;
    pending?: string;
    prettyStacktraceFilename?: string;
    prettyStacktraceLineNumber?: string;
    prettyStacktraceColumnNumber?: string;
    prettyStacktraceError?: string;
  };
  prefixes?: {
    successful?: string;
    failed?: string;
    pending?: string;
  };
  stacktrace?: {
    filter?(stacktrace: string): string;
  };
  customProcessors?: (typeof DisplayProcessor)[];
  customOptions?: any;
  print?: (log: string) => void;
}

enum StacktraceOption {
  NONE = "none",
  RAW = "raw",
  PRETTY = "pretty"
}

Configuration Example:

const config: Configuration = {
  suite: {
    displayNumber: true
  },
  spec: {
    displayErrorMessages: true,
    displayStacktrace: StacktraceOption.PRETTY,
    displaySuccessful: false,
    displayFailed: true,
    displayPending: true,
    displayDuration: true
  },
  summary: {
    displayErrorMessages: false,
    displayStacktrace: StacktraceOption.NONE,
    displaySuccessful: false,
    displayFailed: true,
    displayPending: false,
    displayDuration: true
  },
  colors: {
    enabled: true,
    successful: "green",
    failed: "red",
    pending: "cyan",
    prettyStacktraceFilename: "magenta",
    prettyStacktraceLineNumber: "yellow",
    prettyStacktraceColumnNumber: "yellow",
    prettyStacktraceError: "red"
  },
  prefixes: {
    successful: "✓ ",
    failed: "✗ ",
    pending: "- "
  },
  stacktrace: {
    filter: (stacktrace: string) => stacktrace.replace(/\\/g, "/")
  },
  print: (log: string) => process.stdout.write(log + "\n")
};

const reporter = new SpecReporter(config);

Default Configuration Values:

const defaultConfiguration: Configuration = {
  colors: {
    enabled: true,
    failed: "red",
    pending: "yellow",
    successful: "green",
    prettyStacktraceFilename: "cyan",
    prettyStacktraceLineNumber: "yellow",
    prettyStacktraceColumnNumber: "yellow",
    prettyStacktraceError: "red"
  },
  prefixes: {
    failed: "✗ ",        // "× " on Windows
    pending: "* ",
    successful: "✓ "     // "√ " on Windows
  },
  spec: {
    displayDuration: false,
    displayErrorMessages: true,
    displayFailed: true,
    displayPending: false,
    displayStacktrace: StacktraceOption.NONE,
    displaySuccessful: true
  },
  suite: {
    displayNumber: false
  },
  summary: {
    displayDuration: true,
    displayErrorMessages: true,
    displayFailed: true,
    displayPending: true,
    displayStacktrace: StacktraceOption.NONE,
    displaySuccessful: false
  }
};

Custom Display Processors

Base class and system for creating custom display processors to control output formatting.

class DisplayProcessor {
  protected configuration: Configuration;
  protected theme: Theme;
  
  /**
   * Creates a new display processor instance
   * @param configuration - Reporter configuration
   * @param theme - Color theme instance
   */
  constructor(configuration: Configuration, theme: Theme);
  
  /**
   * Process Jasmine started message
   * @param info - Jasmine suite information
   * @param log - Original log message
   * @returns Processed log message
   */
  displayJasmineStarted(info: jasmine.SuiteInfo, log: string): string;
  
  /**
   * Process suite message
   * @param suite - Suite result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displaySuite(suite: CustomReporterResult, log: string): string;
  
  /**
   * Process spec started message
   * @param spec - Spec result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displaySpecStarted(spec: CustomReporterResult, log: string): string;
  
  /**
   * Process successful spec message
   * @param spec - Spec result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displaySuccessfulSpec(spec: CustomReporterResult, log: string): string;
  
  /**
   * Process failed spec message
   * @param spec - Spec result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displayFailedSpec(spec: CustomReporterResult, log: string): string;
  
  /**
   * Process spec error messages
   * @param spec - Spec result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displaySpecErrorMessages(spec: CustomReporterResult, log: string): string;
  
  /**
   * Process summary error messages
   * @param spec - Spec result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displaySummaryErrorMessages(spec: CustomReporterResult, log: string): string;
  
  /**
   * Process pending spec message
   * @param spec - Spec result information
   * @param log - Original log message
   * @returns Processed log message
   */
  displayPendingSpec(spec: CustomReporterResult, log: string): string;
}

Custom Processor Example:

import { DisplayProcessor, StacktraceOption } from "jasmine-spec-reporter";

class CustomProcessor extends DisplayProcessor {
  public displayJasmineStarted(info: jasmine.SuiteInfo, log: string): string {
    return `Custom Test Suite: ${log}`;
  }
  
  public displaySuccessfulSpec(spec: CustomReporterResult, log: string): string {
    return `✅ ${log} (${spec._jsr?.formattedDuration || 'N/A'})`;
  }
  
  public displayFailedSpec(spec: CustomReporterResult, log: string): string {
    return `❌ ${this.theme.failed(log)}`;
  }
}

// Use the custom processor
const reporter = new SpecReporter({
  spec: {
    displayStacktrace: StacktraceOption.NONE
  },
  customProcessors: [CustomProcessor]
});

Types

Reporter Result Types

interface CustomReporterResult extends jasmine.CustomReporterResult {
  _jsr?: {
    formattedDuration?: string;
  };
}

interface ExecutedSpecs {
  failed: CustomReporterResult[];
  pending: CustomReporterResult[];
  successful: CustomReporterResult[];
}

Theme System

class Theme {
  /**
   * Creates a new theme instance and configures colors
   * @param configuration - Configuration object containing color settings
   */
  constructor(configuration: Configuration);
  
  /**
   * Apply successful spec color formatting
   * @param str - String to format
   * @returns Formatted string with successful color
   */
  successful(str: string): string;
  
  /**
   * Apply failed spec color formatting
   * @param str - String to format
   * @returns Formatted string with failed color
   */
  failed(str: string): string;
  
  /**
   * Apply pending spec color formatting
   * @param str - String to format
   * @returns Formatted string with pending color
   */
  pending(str: string): string;
  
  /**
   * Apply stacktrace filename color formatting
   * @param str - Filename string to format
   * @returns Formatted filename with specified color
   */
  prettyStacktraceFilename(str: string): string;
  
  /**
   * Apply stacktrace line number color formatting
   * @param str - Line number string to format
   * @returns Formatted line number with specified color
   */
  prettyStacktraceLineNumber(str: string): string;
  
  /**
   * Apply stacktrace column number color formatting
   * @param str - Column number string to format
   * @returns Formatted column number with specified color
   */
  prettyStacktraceColumnNumber(str: string): string;
  
  /**
   * Apply stacktrace error color formatting
   * @param str - Error string to format
   * @returns Formatted error with specified color
   */
  prettyStacktraceError(str: string): string;
}

Execution Metrics

class ExecutionMetrics {
  successfulSpecs: number;
  failedSpecs: number;
  pendingSpecs: number;
  skippedSpecs: number;
  totalSpecsDefined: number;
  executedSpecs: number;
  globalErrors: CustomReporterResult[];
  duration: string;
  random: boolean;
  seed: string;
  
  /**
   * Start execution metrics tracking
   * @param suiteInfo - Jasmine suite information containing total specs defined
   */
  start(suiteInfo: jasmine.SuiteInfo): void;
  
  /**
   * Stop execution metrics tracking and calculate final metrics
   * @param runDetails - Jasmine run details containing execution order information
   */
  stop(runDetails: jasmine.RunDetails): void;
  
  /**
   * Start timing for an individual spec
   */
  startSpec(): void;
  
  /**
   * Stop timing for an individual spec and store formatted duration
   * @param result - Spec result to attach duration information to
   */
  stopSpec(result: CustomReporterResult): void;
}

Advanced Usage

Multiple Custom Processors

import { SpecReporter, DisplayProcessor } from "jasmine-spec-reporter";

class TimestampProcessor extends DisplayProcessor {
  public displayJasmineStarted(info: jasmine.SuiteInfo, log: string): string {
    const timestamp = new Date().toISOString();
    return `[${timestamp}] ${log}`;
  }
}

class PrefixProcessor extends DisplayProcessor {
  public displaySuccessfulSpec(spec: CustomReporterResult, log: string): string {
    return `PASS: ${log}`;
  }
  
  public displayFailedSpec(spec: CustomReporterResult, log: string): string {
    return `FAIL: ${log}`;
  }
}

jasmine.getEnv().addReporter(new SpecReporter({
  customProcessors: [TimestampProcessor, PrefixProcessor]
}));

Protractor Integration

// In protractor.conf.js
const { SpecReporter } = require("jasmine-spec-reporter");

exports.config = {
  onPrepare: function() {
    jasmine.getEnv().addReporter(new SpecReporter({
      spec: {
        displayStacktrace: "raw"
      },
      summary: {
        displayDuration: false
      }
    }));
  }
};

Custom Print Function

import * as fs from "fs";

const logFile = fs.createWriteStream("test-results.log", { flags: "a" });

const reporter = new SpecReporter({
  print: (log: string) => {
    // Write to both console and file
    process.stdout.write(log + "\n");
    logFile.write(log + "\n");
  }
});