Documentation Home
This page is for an older version of Jasmine (5.3)
The current stable version of Jasmine is: 5.4 - You can also look at the docs for the next release: Edge

Namespace: jasmine

jasmine

Members of the jasmine global.

Note: The members of the asymmetricEqualityTesters namespace are also accessed via the jasmine global, but due to jsdoc limitations they are not listed here.

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) 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>

The "real" function. This will be used for subsequent calls to the spy after you call mySpy.and.callThrough(). In most cases you should omit this parameter. The usual way to supply an original function is to call spyOn instead of createSpy.

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) getEnv() → {Env}

Get the currently booted Jasmine Environment.

Since:
  • 1.3.0
Returns:
Type
Env

(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) 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.

When the JavaScript runtime reports an uncaught error or unhandled rejection, the spy will be called with a single parameter representing Jasmine's best effort at describing the error. This parameter may be of any type, because JavaScript allows anything to be thrown or used as the reason for a rejected promise, but Error instances and strings are most common.

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 all of the error/rejection events that you want to handle have occurred before you resolve the promise returned from the callback.

You must ensure that the it/beforeEach/etc fn that called spyOnGlobalErrorsAsync does not signal completion until after the promise returned by spyOnGlobalErrorsAsync is resolved. Normally this is done by awaiting the returned promise. Leaving the global error spy installed after the it/beforeEach/etc fn that installed it signals completion is likely to cause problems and is not supported.

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);
  });
});