Matchers that come with Jasmine out of the box.
expect nothing explicitly.
expect().nothing();
expect the actual value to be === to the expected value.
| Name | Type | Description |
|---|---|---|
expected | Object | The expected value to compare against. |
expect(thing).toBe(realThing);
expect the actual value to be within a specified precision of the expected value.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
expected | Object | The expected value to compare against. | ||
precision | Number | <optional> | 2 | The number of decimal points to check. |
expect(number).toBeCloseTo(42.2, 3);
expect the actual value to be defined. (Not undefined)
expect(result).toBeDefined();
expect the actual value to be falsy
expect(result).toBeFalsy();
expect the actual value to be greater than the expected value.
| Name | Type | Description |
|---|---|---|
expected | Number | The value to compare against. |
expect(result).toBeGreaterThan(3);
expect the actual value to be greater than or equal to the expected value.
| Name | Type | Description |
|---|---|---|
expected | Number | The expected value to compare against. |
expect(result).toBeGreaterThanOrEqual(25);
expect the actual value to be less than the expected value.
| Name | Type | Description |
|---|---|---|
expected | Number | The expected value to compare against. |
expect(result).toBeLessThan(0);
expect the actual value to be less than or equal to the expected value.
| Name | Type | Description |
|---|---|---|
expected | Number | The expected value to compare against. |
expect(result).toBeLessThanOrEqual(123);
expect the actual value to be NaN (Not a Number).
expect(thing).toBeNaN();
expect the actual value to be -Infinity (-infinity).
expect(thing).toBeNegativeInfinity();
expect the actual value to be null.
expect(result).toBeNull();
expect the actual value to be Infinity (infinity).
expect(thing).toBePositiveInfinity();
expect the actual value to be truthy.
expect(thing).toBeTruthy();
expect the actual value to be undefined.
expect(result).toBeUndefined():
expect the actual value to contain a specific value.
| Name | Type | Description |
|---|---|---|
expected | Object | The value to look for. |
expect(array).toContain(anElement); expect(string).toContain(substring);
expect the actual value to be equal to the expected, using deep equality comparison.
| Name | Type | Description |
|---|---|---|
expected | Object | Expected value |
expect(bigObject).toEqual({"foo": ['bar', 'baz']}); expect the actual (a Spy) to have been called.
expect(mySpy).toHaveBeenCalled(); expect(mySpy).not.toHaveBeenCalled();
expect the actual value (a Spy) to have been called before another Spy.
| Name | Type | Description |
|---|---|---|
expected | Spy |
expect(mySpy).toHaveBeenCalledBefore(otherSpy);
expect the actual (a Spy) to have been called the specified number of times.
| Name | Type | Description |
|---|---|---|
expected | Number | The number of invocations to look for. |
expect(mySpy).toHaveBeenCalledTimes(3);
expect the actual (a Spy) to have been called with particular arguments at least once.
| Type | Attributes | Description |
|---|---|---|
| Object | <repeatable> | The arguments to look for |
expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2); expect the actual value to be a DOM element that has the expected class
| Name | Type | Description |
|---|---|---|
expected | Object | The class name to test for |
var el = document.createElement('div');
el.className = 'foo bar baz';
expect(el).toHaveClass('bar'); expect the actual value to match a regular expression
| Name | Type | Description |
|---|---|---|
expected | RegExp | String | Value to look for in the string. |
expect("my string").toMatch(/string$/);
expect("other string").toMatch("her"); expect a function to throw something.
| 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. |
expect(function() { return 'things'; }).toThrow('foo');
expect(function() { return 'stuff'; }).toThrow(); expect a function to throw an Error.
| Name | Type | Attributes | Description |
|---|---|---|---|
expected | Error | <optional> |
|
message | RegExp | String | <optional> | The message that should be set on the thrown |
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(); expect a function to throw something matching a predicate.
| Name | Type | Description |
|---|---|---|
predicate | function | A function that takes the thrown exception as its parameter and returns true if it matches. |
expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });
© 2008–2017 Pivotal Labs
Licensed under the MIT License.
https://jasmine.github.io/api/3.2/matchers.html