Matchers
Matchers are used to compare the expected and actual values or validation of the expected value. If a matcher is going to compare the expected value, it takes actual value as argument. Matchers are chained with expectations. There are a number of matchers provided by Jasmine. Here is a list of all matchers:
toBe()
: This matcher compares using the identity (= = =
) operator.var a = 5; expect(a).toBe(5); expect(a).not.toBe("5");
The difference between the identity operator (
= = =
) and equality operator (= =
) is that in identity operator, no type conversion is done before comparison.toEqual()
: This matcher is used to compare simple literals, variables, and functions.expect(a).toEqual(5); // checking for simple variables. expect(a).not.toEqual(3);
For simple literals and variables, both
toBe()
andtoEqual()
can be used, but to compare objects onlytoEqual()
is to be used.toMatch()
: This matcher is used for regular expressions.var strReg = "The quick brown fox"; expect(strReg).toMatch...