Jasmine Matchers

In the first Jasmine example, we used the toEqual and toThrow Jasmine Matchers. In the following table, some of the other built-in matchers provided by Jasmine are explained briefly:

Matcher

Description

expect(x).toBe(y)

The toBe matcher passes if x is of the same type and value of y. The toBe matcher uses === to perform this comparison.

expect(x).toBeDefined()

The toBeDefined matcher is used to ensure that x is defined.

expect(x).toBeUndefined()

The toBeUndefined matcher is used to ensure that x is undefined.

expect(x).toBeNull()

The toBeNull matcher is used to ensure that x is null.

expect(x).toBeTruthy()

The toBeTruthy matcher is used to ensure that x is truthy.

expect(x).toBeFalsy()

The toBeFalsy matcher is used to ensure that x is falsy.

expect(x).toContain(y)

The toContain matcher is used to check whether the x string or array value contains y. A valid y value can be a substring of x or an item of x.

expect(x).toBeLessThan(y)

The toBeLessThan matcher is used to ensure that x is less than y.

expect(x).toBeGreaterThan(y)

The toBeGreaterThan matcher is used to ensure that x is greater than y.

expect(x).toMatch(y)

The toMatch matcher is used to check whether x matches a string or regular expression (y).

Tip

Adding to built-in matchers, you can create your own Jasmine custom matcher. To create your own Jasmine custom matcher, check the Jasmine 2.0 custom matcher documentation page at http://jasmine.github.io/2.0/custom_matcher.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.247.53