Advanced Matchers
Learn how to write expectations using matchers and regular expressions.
We'll cover the following...
Jasmine offers a number of matchers that let us write expressive expectations.
The toMatch method
There are two ways to check whether a string value contains a specified string.
-
We can call the
toMatchmethod with our search string. -
We can call the
toContainmethod with our search string.
Take a look at the code example below. These two expectations are equivalent:
expect("<h1>Cafe Americano (dairy-free)</h1>").toMatch("Americano");expect("<h1>Cafe Americano (dairy-free)</h1>").toContain("Americano");
We can also use the toMatch method to let us match a
/ symbol, but we do not use quotation marks. In the second example, we use /i, which is the case-insensitive flag.
expect("<h1>Cafe Americano (dairy-free)</h1>").toMatch(/Americano/);expect("<h1>Cafe Americano (dairy-free)</h1>").toMatch(/americano/i);
Regular expressions
Matching with regular ...