To understand this recipe, let's assume that you are developing an application and you have to implement test code for various scenarios by applying different Jasmine matchers.
Let's consider some scenarios in the preceding context, that is, where Jasmine matchers should be applied for different test conditions:
Let's take a look at the steps of this recipe.
In steps 1 to 3, we implemented scenario 1 with the toMatch
matcher. It checks whether something is matched for a regular expression. You can use the toMatch
matcher to test search patterns. In our case, we implemented the test code to find out search patterns with different regular expressions.
In steps 4 and 5, we defined the specs for scenario 2 and 3, and we implemented the test code corresponding to specs using the toEqual
and toBe
matchers. Here, notice that toBe
looks similar to toEqual
, but that is not the case. The toBe
matcher returns the value true
if the objects are equal. For example, in our case, MyObj
and MySameObj
look like the same objects, but in fact both are different objects with exactly the same attribute/behavior. Therefore, the assertion will return a true
value with the toEqual
matcher, but a false
value with the toBe
matcher.
In steps 6 to 8, we implemented scenarios 4 and 5 and saw how the toBeDefine
and toBeUndefine
matchers are applied to test JavaScript's Undefined
type. In step 7, we first implemented a test condition for a non-empty string and two test conditions with object variable MyObj
. We also implemented test conditions for the strUndefined
variable and the MyFunction()
function by applying negative assertions. Conversely, in step 8, we implemented test conditions with the object variable MyObj
by applying negative assertions.
In step 9, we implemented test code for scenario 6 using the toBeNull
matcher. In step 10, we saw test conditions pass for null
values. However, we applied negative assertions to pass test conditions for not Null
and Undefined
values.
In step 11, we implemented scenario 7 and scenario 8 using the toBeTruthy
and toBeFalsy
matchers. We use the toBeTruthy
matcher to check whether something returns/evaluates to true
. Similarly, we use the toBeFalsy
matcher to check whether something returns/evaluates to false. In our case, we applied the toBeTruthy
matcher for true value, non-empty strings and numbers other than zero. Similarly, we applied toBeFalsy
matcher to validate false, null, and empty strings.
In step 13, we implemented the test code for scenario 9 using the toContain
matcher. Here, we implemented test conditions to find out an element(s)/item(s) of an array using the toContain
matcher. Similarly, we implemented test conditions to check if an element/an item does did not exist in an array by applying negative assertions.
In step 15, we implemented scenario 10 and scenario 11 to compare mathematical values using the toBeLessThan
and toBeGreaterThan
matchers.
In step 17, we implemented scenario 12 using the toBeCloseTo
matcher. This matcher is used to check whether a number is close to another number, up to a given level of decimal precision. In our case, we checked whether the expected number was equal to the actual number with a given level of decimal precision.