Search⌘ K
AI Features

Advanced Matchers

Explore how to use advanced Jasmine matchers including toMatch with regular expressions to write expressive and precise expectations in Angular unit tests. Learn to validate string formats such as currency and improve your testing flexibility.

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 toMatch method with our search string.

  • We can call the toContain method with our search string.

Take a look at the code example below. These two expectations are equivalent:

TypeScript 3.3.4
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 regular expressionA regular expression is a string representing a search pattern. JavaScript supports regular expressions with the native RegExp class.. In the following code block, notice how we wrap our regular expression with the / symbol, but we do not use quotation marks. In the second example, we use /i, which is ...