Documentation Home
This page describes a pre-release version of Jasmine (5.0.0-beta.0). There may be additional changes, including breaking changes, before the final 5.0.0 release.
The current stable version of Jasmine is 5.5.

Namespace: jasmine

jasmine

Members

(static) DEFAULT_TIMEOUT_INTERVAL

Default number of milliseconds Jasmine will wait for an asynchronous spec, before, or after function to complete. This can be overridden on a case by case basis by passing a time limit as the third argument to it, beforeEach, afterEach, beforeAll, or afterAll. The value must be no greater than the largest number of milliseconds supported by setTimeout, which is usually 2147483647.

While debugging tests, you may want to set this to a large number (or pass a large number to one of the functions mentioned above) so that Jasmine does not move on to after functions or the next spec while you're debugging.

Since:
  • 1.3.0
Default Value:
  • 5000

(static) MAX_PRETTY_PRINT_ARRAY_LENGTH

Maximum number of array elements to display when pretty printing objects. This will also limit the number of keys and values displayed for an object. Elements past this number will be ellipised.

Since:
  • 2.7.0
Default Value:
  • 50

(static) MAX_PRETTY_PRINT_CHARS

Maximum number of characters to display when pretty printing objects. Characters past this number will be ellipised.

Since:
  • 2.9.0
Default Value:
  • 100

(static) MAX_PRETTY_PRINT_DEPTH

Maximum object depth the pretty printer will print to. Set this to a lower value to speed up pretty printing if you have large objects.

Since:
  • 1.3.0
Default Value:
  • 8

Methods

(static) addAsyncMatchers(matchers)

Add custom async matchers for the current scope of specs.

Note: This is only callable from within a beforeEach, it, or beforeAll.

Parameters:
Name Type Description
matchers Object

Keys from this object will be the new async matcher names.

Since:
  • 3.5.0
See:

(static) addCustomEqualityTester(tester)

Add a custom equality tester for the current scope of specs.

Note: This is only callable from within a beforeEach, it, or beforeAll.

Parameters:
Name Type Description
tester function

A function which takes two arguments to compare and returns a true or false comparison result if it knows how to compare them, and undefined otherwise.

Since:
  • 2.0.0
See:

(static) addCustomObjectFormatter(formatter)

Add a custom object formatter for the current scope of specs.

Note: This is only callable from within a beforeEach, it, or beforeAll.

Parameters:
Name Type Description
formatter function

A function which takes a value to format and returns a string if it knows how to format it, and undefined otherwise.

Since:
  • 3.6.0
See:

(static) addMatchers(matchers)

Add custom matchers for the current scope of specs.

Note: This is only callable from within a beforeEach, it, or beforeAll.

Parameters:
Name Type Description
matchers Object

Keys from this object will be the new matcher names.

Since:
  • 2.0.0
See:

(static) addSpyStrategy(name, factory)

Add a custom spy strategy for the current scope of specs.

Note: This is only callable from within a beforeEach, it, or beforeAll.

Parameters:
Name Type Description
name String

The name of the strategy (i.e. what you call from and)

factory function

Factory function that returns the plan to be executed.

Since:
  • 3.5.0

(static) any(clazz)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared is an instance of the specified class/constructor.

Parameters:
Name Type Description
clazz Constructor

The constructor to check against.

Since:
  • 1.3.0

(static) anything()

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared is not null and not undefined.

Since:
  • 2.2.0

(static) arrayContaining(sample)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value is an Array that contains at least the elements in the sample.

Parameters:
Name Type Description
sample Array
Since:
  • 2.2.0

(static) arrayWithExactContents(sample)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value is an Array that contains all of the elements in the sample in any order.

Parameters:
Name Type Description
sample Array
Since:
  • 2.8.0

(static) clock() → {Clock}

Get the currently booted mock {Clock} for this Jasmine environment.

Since:
  • 2.0.0
Returns:
Type
Clock

(static) createSpy(nameopt, originalFnopt) → {Spy}

Create a bare Spy object. This won't be installed anywhere and will not have any implementation behind it.

Parameters:
Name Type Attributes Description
name String <optional>

Name to give the spy. This will be displayed in failure messages.

originalFn function <optional>

Function to act as the real implementation.

Since:
  • 1.3.0
Returns:
Type
Spy

(static) createSpyObj(baseNameopt, methodNames, propertyNamesopt) → {Object}

Create an object with multiple Spys as its members.

Parameters:
Name Type Attributes Description
baseName String <optional>

Base name for the spies in the object.

methodNames Array.<String> | Object

Array of method names to create spies for, or Object whose keys will be method names and values the returnValue.

propertyNames Array.<String> | Object <optional>

Array of property names to create spies for, or Object whose keys will be propertynames and values the returnValue.

Since:
  • 1.3.0
Returns:
Type
Object

(static) debugLog(msg)

Logs a message for use in debugging. If the spec fails, trace messages will be included in the result passed to the reporter's specDone method.

This method should be called only when a spec (including any associated beforeEach or afterEach functions) is running.

Parameters:
Name Type Description
msg String

The message to log

Since:
  • 4.0.0

(static) empty()

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared is empty.

Since:
  • 3.1.0

(static) falsy()

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared is null, undefined, 0, false or anything falsey.

Since:
  • 3.1.0

(static) getEnv() → {Env}

Get the currently booted Jasmine Environment.

Since:
  • 1.3.0
Returns:
Type
Env

(static) is(sample)

Get an AsymmetricEqualityTester, usable in any matcher that passes if the actual value is the same as the sample as determined by the === operator.

Parameters:
Name Type Description
sample Object

The value to compare the actual to.

(static) isSpy(putativeSpy) → {Boolean}

Determines whether the provided function is a Jasmine spy.

Parameters:
Name Type Description
putativeSpy function

The function to check.

Since:
  • 2.0.0
Returns:
Type
Boolean

(static) mapContaining(sample)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if every key/value pair in the sample passes the deep equality comparison with at least one key/value pair in the actual value being compared

Parameters:
Name Type Description
sample Map

The subset of items that must be in the actual.

Since:
  • 3.5.0

(static) notEmpty()

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared is not empty.

Since:
  • 3.1.0

(static) objectContaining(sample)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared contains at least the keys and values.

Parameters:
Name Type Description
sample Object

The subset of properties that must be in the actual.

Since:
  • 1.3.0

(static) setContaining(sample)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if every item in the sample passes the deep equality comparison with at least one item in the actual value being compared

Parameters:
Name Type Description
sample Set

The subset of items that must be in the actual.

Since:
  • 3.5.0

(static) setDefaultSpyStrategy(defaultStrategyFn)

Set the default spy strategy for the current scope of specs.

Note: This is only callable from within a beforeEach, it, or beforeAll.

Parameters:
Name Type Description
defaultStrategyFn function

a function that assigns a strategy

Example
beforeEach(function() {
  jasmine.setDefaultSpyStrategy(and => and.returnValue(true));
});

(async, static) spyOnGlobalErrorsAsync(fn)

Replaces Jasmine's global error handling with a spy. This prevents Jasmine from treating uncaught exceptions and unhandled promise rejections as spec failures and allows them to be inspected using the spy's calls property and related matchers such as toHaveBeenCalledWith.

After installing the spy, spyOnGlobalErrorsAsync immediately calls its argument, which must be an async or promise-returning function. The spy will be passed as the first argument to that callback. Normal error handling will be restored when the promise returned from the callback is settled.

Note: The JavaScript runtime may deliver uncaught error events and unhandled rejection events asynchronously, especially in browsers. If the event occurs after the promise returned from the callback is settled, it won't be routed to the spy even if the underlying error occurred previously. It's up to you to ensure that the returned promise isn't resolved until all of the error/rejection events that you want to handle have occurred.

You must await the return value of spyOnGlobalErrorsAsync.

Parameters:
Name Type Description
fn AsyncFunction

A function to run, during which the global error spy will be effective

Example
it('demonstrates global error spies', async function() {
  await jasmine.spyOnGlobalErrorsAsync(async function(globalErrorSpy) {
    setTimeout(function() {
      throw new Error('the expected error');
    });
    await new Promise(function(resolve) {
      setTimeout(resolve);
    });
    const expected = new Error('the expected error');
    expect(globalErrorSpy).toHaveBeenCalledWith(expected);
  });
});

(static) stringContaining(expected)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value is a String that contains the specified String.

Parameters:
Name Type Description
expected String
Since:
  • 3.10.0

(static) stringMatching(expected)

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value is a String that matches the RegExp or String.

Parameters:
Name Type Description
expected RegExp | String
Since:
  • 2.2.0

(static) truthy()

Get an AsymmetricEqualityTester, usable in any matcher that uses Jasmine's equality (e.g. toEqual, toContain, or toHaveBeenCalledWith), that will succeed if the actual value being compared is true or anything truthy.

Since:
  • 3.1.0