Jasmine JavaScript Testing Framework Documentation

repository·main·Indexed Apr 15, 2026

https://github.com/jasmine/jasmine

Jasmine is a Behavior Driven Development (BDD) testing framework for JavaScript that runs in Node.js and browsers without dependencies on the DOM or external frameworks. Supported environments include Node 20, 22, 24, and evergreen versions of Chrome, Firefox, Edge, and Safari. Key features include a natural language syntax, robust error handling, and a deprecation warning system. The framework provides jasmine.clock() for mocking timers (setTimeout, setInterval) and the Date object, allowing manual time advancement via tick() or auto-tick mode. It includes an ExceptionFormatter for readable stack traces and a Deprecator class to manage warnings about unsupported patterns like monkey-patching.

Tokens
75.4K
Snippets
276
Records
455
Agent score
97%

What's inside jasmine

  1. Warning Context

    main

    Deprecation warnings include context about where they occurred:

    • If the warning is at the top suite level, no context is added.
    • If it's in a suite, the suite's full name is included.
    • If it's in a spec, the spec's full name is included.
    • A stack trace is included unless omitStackTrace is set to true.
  2. Access Spec Metadata and Properties

    main

    You can access metadata about the current spec via the metadata property on the spec object. This provides read-only access to key identification and description details without exposing the full spec instance.

    Available metadata properties:

    • id: The unique ID of the spec (string).
    • description: The description passed to the it block that created this spec (string).
    • getFullName(): Returns the full description including all ancestor suites (string).
    • getPath(): Returns the full path of the spec as an array of names (available since v5.7.0).
    • filename: The name of the file the spec was defined in (available since v5.13.0).

    Note on filename: The value may be incorrect if zone.js is installed or if it/fit/xit have been replaced with versions that do not maintain the same call stack height. To fix this, set the extraItStackFrames configuration option.

    Usage:

    // Inside a spec or reporter
    const specMetadata = this.metadata;
    console.log(specMetadata.id);
    console.log(specMetadata.getFullName());
    console.log(specMetadata.getPath());

    Sources: lib/jasmine-core/jasmine.js

  3. Understand Suite metadata for reporting

    main

    The Suite object exposes a metadata property that provides read-only information about the test suite, useful for reporters or debugging.

    The SuiteMetadata object contains:

    • id: The unique ID of the suite.
    • parentSuite: The parent suite object (or null if this is the top suite).
    • description: The description passed to the describe block.
    • filename: The name of the file where the suite was defined. Note: This value may be incorrect if zone.js is installed or if describe/fdescribe/xdescribe have been replaced with versions that don't maintain the same call stack height. You can fix this by setting Configuration#extraItStackFrames.
    • getFullName(): Returns the full description including all ancestors.
    • children: An array of child specs or suites (returned as their metadata objects).

    Example usage:

    describe('My Suite', function() {
      it('has metadata', function() {
        const meta = this.currentSuite.metadata;
        console.log(meta.id, meta.description, meta.getFullName());
      });
    });

    Sources: lib/jasmine-core/jasmine.js

  4. Usage

    main
    it('matches all conditions', () => {
      const value = { name: 'Alice', age: 30 };
      expect(value).toEqual(jasmine.allOf(
        jasmine.objectContaining({ name: 'Alice' }),
        jasmine.objectContaining({ age: jasmine.any(Number) })
      ));
    });
  5. Use jasmine.empty to match empty collections

    main

    Use jasmine.empty() to assert that a value is an empty collection (string, array, typed array, Map, Set, or object with no keys).

    Supported Types:

    • Strings, Arrays, TypedArrays: length === 0
    • Maps, Sets: size === 0
    • Objects: Object.keys(obj).length === 0

    Usage:

    expect([]).toEqual(jasmine.empty());
    expect({}).toEqual(jasmine.empty());
    expect(new Map()).toEqual(jasmine.empty());
    expect('').toEqual(jasmine.empty());

    Note: This matcher returns false for non-collection types (e.g., numbers, booleans, null).

    Sources: lib/jasmine-core/jasmine.js

  6. Use jasmine.truthy to match truthy values

    main

    Use jasmine.truthy() to assert that a value is truthy (evaluates to true in a boolean context).

    Truthy Values: Any value except false, 0, '', null, undefined, NaN

    Usage:

    expect(1).toEqual(jasmine.truthy());
    expect('hello').toEqual(jasmine.truthy());
    expect({}).toEqual(jasmine.truthy());

    Note: This is the inverse of jasmine.falsy().

    Sources: lib/jasmine-core/jasmine.js

  7. Use toBeResolvedTo to assert resolved value equality

    main

    Use toBeResolvedTo to assert that a promise resolves to a specific value using deep equality comparison. This matcher checks both that the promise resolves and that the resolved value equals the expected value.

    Usage:

    await expectAsync(aPromise).toBeResolvedTo({prop: 'value'});
    return expectAsync(aPromise).toBeResolvedTo(42);

    If the promise rejects, the test fails with a message showing the rejection value. If the promise resolves to a different value, the test fails with a message showing both the expected and actual resolved values.

    Sources: lib/jasmine-core/jasmine.js