Namespace: matchers

matchers

Matchers that come with Jasmine out of the box.
Source:

Methods

nothing()

expect nothing explicitly.
Source:
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.
Source:
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.
Source:
Example
expect(number).toBeCloseTo(42.2, 3);

toBeDefined()

expect the actual value to be defined. (Not `undefined`)
Source:
Example
expect(result).toBeDefined();

toBeFalsy()

expect the actual value to be falsy
Source:
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.
Source:
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.
Source:
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.
Source:
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.
Source:
Example
expect(result).toBeLessThanOrEqual(123);

toBeNaN()

expect the actual value to be `NaN` (Not a Number).
Source:
Example
expect(thing).toBeNaN();

toBeNegativeInfinity()

expect the actual value to be `-Infinity` (-infinity).
Source:
Example
expect(thing).toBeNegativeInfinity();

toBeNull()

expect the actual value to be `null`.
Source:
Example
expect(result).toBeNull();

toBePositiveInfinity()

expect the actual value to be `Infinity` (infinity).
Source:
Example
expect(thing).toBePositiveInfinity();

toBeTruthy()

expect the actual value to be truthy.
Source:
Example
expect(thing).toBeTruthy();

toBeUndefined()

expect the actual value to be `undefined`.
Source:
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.
Source:
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
Source:
Example
expect(bigObject).toEqual({"foo": ['bar', 'baz']});

toHaveBeenCalled()

expect the actual (a Spy) to have been called.
Source:
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.
Source:
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.
Source:
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
Source:
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
Source:
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.
Source:
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.
Source:
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`
Source:
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.
Source:
Example
expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });