or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

custom-extensions.mdenvironment-config.mdexpectations-matchers.mdindex.mdspy-system.mdtest-organization.mdtest-utilities.md
tile.json

tessl/npm-jasmine-core

Simple JavaScript testing framework for browsers and node.js

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jasmine-core@4.6.x

To install, run

npx @tessl/cli install tessl/npm-jasmine-core@4.6.0

index.mddocs/

Jasmine Core

Jasmine Core is a behavior-driven development (BDD) testing framework for JavaScript that runs anywhere JavaScript can run. It provides an intuitive describe/it syntax for organizing test suites, powerful assertion matchers, flexible spy functionality for mocking, and asynchronous testing support. The framework operates independently of browsers, DOM, or any specific JavaScript framework.

Package Information

  • Package Name: jasmine-core
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install jasmine-core

Core Imports

Node.js with global setup:

const jasmine = require('jasmine-core');
jasmine.boot(); // Adds describe, it, expect, etc. to global scope

Node.js without globals:

const { describe, it, expect, beforeEach, afterEach } = require('jasmine-core').noGlobals();

Browser (script tags):

<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot0.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot1.js"></script>

Basic Usage

const { describe, it, expect, beforeEach } = require('jasmine-core').noGlobals();

describe('Calculator', () => {
  let calculator;

  beforeEach(() => {
    calculator = {
      add: (a, b) => a + b,
      multiply: (a, b) => a * b
    };
  });

  it('should add two numbers correctly', () => {
    expect(calculator.add(2, 3)).toEqual(5);
  });

  it('should multiply two numbers correctly', () => {
    expect(calculator.multiply(4, 5)).toEqual(20);
  });
});

Architecture

Jasmine Core is built around several key components:

  • Test Organization: Hierarchical structure using describe (suites) and it (specs) for organizing tests
  • Expectation System: Fluent assertion API using expect() with chainable matchers
  • Spy System: Comprehensive mocking and spying functionality for testing object interactions
  • Asynchronous Testing: Built-in support for promises, callbacks, and async/await patterns
  • Environment Management: Configurable test execution environment with randomization and timeout controls
  • Reporter Interface: Pluggable reporting system for various output formats

Capabilities

Test Organization and Structure

Core functions for defining test suites, individual tests, and setup/teardown hooks. Provides the fundamental structure for organizing and executing tests.

function describe(description: string, specDefinitions: () => void): Suite;
function it(description: string, testFunction?: () => void | Promise<void>, timeout?: number): Spec;
function beforeEach(action: () => void | Promise<void>, timeout?: number): void;
function afterEach(action: () => void | Promise<void>, timeout?: number): void;
function beforeAll(action: () => void | Promise<void>, timeout?: number): void;
function afterAll(action: () => void | Promise<void>, timeout?: number): void;

Test Organization

Expectations and Matchers

Assertion system for verifying expected behavior. Includes synchronous and asynchronous expectations with a comprehensive set of built-in matchers.

function expect(actual: any): Expectation;
function expectAsync(actual: Promise<any>): AsyncExpectation;

interface Expectation {
  toBe(expected: any): boolean;
  toEqual(expected: any): boolean;
  toBeCloseTo(expected: number, precision?: number): boolean;
  toContain(expected: any): boolean;
  toMatch(expected: string | RegExp): boolean;
  toThrow(expected?: any): boolean;
  // ... many more matchers
}

Expectations and Matchers

Spy System

Mock and spy functionality for testing object interactions, method calls, and return values. Includes spy creation, behavior configuration, and call tracking.

function spyOn(obj: object, methodName: string): Spy;
function spyOnProperty(obj: object, propertyName: string, accessType?: 'get' | 'set'): Spy;

interface Spy {
  and: SpyStrategy;
  calls: CallTracker;
}

interface SpyStrategy {
  returnValue(value: any): Spy;
  callFake(fn: Function): Spy;
  callThrough(): Spy;
  throwError(msg?: string): Spy;
}

Spy System

Environment and Configuration

Test execution environment management including configuration options, randomization settings, and timeout controls.

interface jasmine {
  getEnv(): Environment;
  DEFAULT_TIMEOUT_INTERVAL: number;
}

interface Environment {
  configure(options: EnvConfig): void;
  execute(): void;
}

interface EnvConfig {
  random?: boolean;
  seed?: string | number;
  stopOnSpecFailure?: boolean;
  failSpecWithNoExpectations?: boolean;
}

Environment and Configuration

Custom Extensions

System for extending Jasmine with custom matchers, equality testers, object formatters, and spy strategies.

interface jasmine {
  addMatchers(matchers: { [matcherName: string]: MatcherFactory }): void;
  addAsyncMatchers(matchers: { [matcherName: string]: AsyncMatcherFactory }): void;
  addCustomEqualityTester(tester: EqualityTester): void;
  addCustomObjectFormatter(formatter: ObjectFormatter): void;
}

Custom Extensions

Test Utilities

Additional testing utilities including asymmetric equality testers, clock mocking, and helper functions for complex testing scenarios.

interface jasmine {
  any(constructor: Function): AsymmetricEqualityTester;
  anything(): AsymmetricEqualityTester;
  objectContaining(sample: object): AsymmetricEqualityTester;
  clock(): Clock;
}

interface Clock {
  install(): void;
  uninstall(): void;
  tick(milliseconds: number): void;
  mockDate(date?: Date): void;
}

Test Utilities

Types

interface Suite {
  id: number;
  description: string;
  fullName: string;
}

interface Spec {
  id: number;
  description: string;
  fullName: string;
  result: SpecResult;
}

interface SpecResult {
  status: 'passed' | 'failed' | 'pending';
  description: string;
  fullName: string;
  failedExpectations: ExpectationResult[];
  passedExpectations: ExpectationResult[];
}

interface ExpectationResult {
  matcherName: string;
  message: string;
  stack: string;
  passed: boolean;
}