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

Namespace: matchers

matchers

Matchers that come with Jasmine out of the box.

Methods

toBe(expected)

expect the actual value to be === to the expected value.

Parameters:
Name Type Description
expected Object

The expected value to compare against.

Example
expect(thing).toBe(realThing);

toBeCloseTo(expected, precisionopt)

expect the actual value to be within a specified precision of the expected value.

Parameters:
Name Type Attributes Default Description
expected Object

The expected value to compare against.

precision Number <optional>
2

The number of decimal points to check.

Example
expect(number).toBeCloseTo(42.2, 3);

toBeDefined()

expect the actual value to be defined. (Not undefined)

Example
expect(result).toBeDefined();

toBeFalsy()

expect the actual value to be falsy

Example
expect(result).toBeFalsy();

toBeGreaterThan(expected)

expect the actual value to be greater than the expected value.

Parameters:
Name Type Description
expected Number

The value to compare against.

Example
expect(result).toBeGreaterThan(3);

toBeGreaterThanOrEqual(expected)

expect the actual value to be greater than or equal to the expected value.

Parameters:
Name Type Description
expected Number

The expected value to compare against.

Example
expect(result).toBeGreaterThanOrEqual(25);

toBeLessThan(expected)

expect the actual value to be less than the expected value.

Parameters:
Name Type Description
expected Number

The expected value to compare against.

Example
expect(result).toBeLessThan(0);

toBeLessThanOrEqual(expected)

expect the actual value to be less than or equal to the expected value.

Parameters:
Name Type Description
expected Number

The expected value to compare against.

Example
expect(result).toBeLessThanOrEqual(123);

toBeNaN()

expect the actual value to be NaN (Not a Number).

Example
expect(thing).toBeNaN();

toBeNull()

expect the actual value to be null.

Example
expect(result).toBeNull();

toBeTruthy()

expect the actual value to be truthy.

Example
expect(thing).toBeTruthy();

toBeUndefined()

expect the actual value to be undefined.

Example
expect(result).toBeUndefined():

toContain(expected)

expect the actual value to contain a specific value.

Parameters:
Name Type Description
expected Object

The value to look for.

Example
expect(array).toContain(anElement);
expect(string).toContain(substring);

toEqual(expected)

expect the actual value to be equal to the expected, using deep equality comparison.

Parameters:
Name Type Description
expected Object

Expected value

Example
expect(bigObject).toEqual({"foo": ['bar', 'baz']});

toHaveBeenCalled()

expect the actual (a Spy) to have been called.

Example
expect(mySpy).toHaveBeenCalled();
expect(mySpy).not.toHaveBeenCalled();

toHaveBeenCalledBefore(expected)

expect the actual value (a Spy) to have been called before another Spy.

Parameters:
Name Type Description
expected Spy

Spy that should have been called after the actual Spy.

Example
expect(mySpy).toHaveBeenCalledBefore(otherSpy);

toHaveBeenCalledTimes(expected)

expect the actual (a Spy) to have been called the specified number of times.

Parameters:
Name Type Description
expected Number

The number of invocations to look for.

Example
expect(mySpy).toHaveBeenCalledTimes(3);

toHaveBeenCalledWith()

expect the actual (a Spy) to have been called with particular arguments at least once.

Parameters:
Type Attributes Description
Object <repeatable>

The arguments to look for

Example
expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2);

toMatch(expected)

expect the actual value to match a regular expression

Parameters:
Name Type Description
expected RegExp | String

Value to look for in the string.

Example
expect("my string").toMatch(/string$/);
expect("other string").toMatch("her");

toThrow(expectedopt)

expect a function to throw something.

Parameters:
Name Type Attributes Description
expected Object <optional>

Value that should be thrown. If not provided, simply the fact that something was thrown will be checked.

Example
expect(function() { return 'things'; }).toThrow('foo');
expect(function() { return 'stuff'; }).toThrow();

toThrowError(expectedopt, messageopt)

expect a function to throw an Error.

Parameters:
Name Type Attributes Description
expected Error <optional>

Error constructor the object that was thrown needs to be an instance of. If not provided, Error will be used.

message RegExp | String <optional>

The message that should be set on the thrown Error

Example
expect(function() { return 'things'; }).toThrowError(MyCustomError, 'message');
expect(function() { return 'things'; }).toThrowError(MyCustomError, /bar/);
expect(function() { return 'stuff'; }).toThrowError(MyCustomError);
expect(function() { return 'other'; }).toThrowError(/foo/);
expect(function() { return 'other'; }).toThrowError();