Matchers that come with Jasmine out of the box.
Methods
nothing()
expect
nothing explicitly.
Example
expect().nothing();
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();
toBeNegativeInfinity()
expect
the actual value to be -Infinity
(-infinity).
Example
expect(thing).toBeNegativeInfinity();
toBeNull()
expect
the actual value to be null
.
Example
expect(result).toBeNull();
toBePositiveInfinity()
expect
the actual value to be Infinity
(infinity).
Example
expect(thing).toBePositiveInfinity();
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()
Example
expect(mySpy).toHaveBeenCalled();
expect(mySpy).not.toHaveBeenCalled();
toHaveBeenCalledBefore(expected)
Parameters:
Name | Type | Description |
---|---|---|
expected |
Spy |
Example
expect(mySpy).toHaveBeenCalledBefore(otherSpy);
toHaveBeenCalledTimes(expected)
Parameters:
Name | Type | Description |
---|---|---|
expected |
Number | The number of invocations to look for. |
Example
expect(mySpy).toHaveBeenCalledTimes(3);
toHaveBeenCalledWith()
Parameters:
Type | Attributes | Description |
---|---|---|
Object |
<repeatable> |
The arguments to look for |
Example
expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2);
toHaveClass(expected)
expect
the actual value to be a DOM element that has the expected class
Parameters:
Name | Type | Description |
---|---|---|
expected |
Object | The class name to test for |
Example
var el = document.createElement('div');
el.className = 'foo bar baz';
expect(el).toHaveClass('bar');
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> |
|
message |
RegExp | String |
<optional> |
The message that should be set on the thrown |
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();
toThrowMatching(predicate)
expect
a function to throw
something matching a predicate.
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function | A function that takes the thrown exception as its parameter and returns true if it matches. |
Example
expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });