What kind of checks can we perform with matcher functions in Angular 2? Well, the answer is: quite a lot. Besides the most common matchers such as to toBe()
or toEqual()
, we can use any built-in Jasmine matcher. Please refer to the Jasmine official site for a complete rundown on the matchers available. On top of that, Angular 2 implements a set of custom matchers to perform common operations when testing Angular 2 specific modules:
toBePromise()
: This expects the value to be a PromisetoBeAnInstanceOf(expected: any)
: This expects the actual value to be an instance of a class defined in the expected argumenttoHaveText(expected: any)
: This expects the element to have exactly the text defined in the expected
argumenttoHaveCssClass(expected: any)
: This expects the element to have the CSS class defined in the expected
argumenttoHaveCssStyle(expected: any)
: This expects the element to be styled with the given CSS styles defined in the payloadtoImplement(expected: any)
: This expects a class to implement the interface of the given classtoContainError(expected: any)
: This expects an exception to contain an expected error texttoThrowErrorWith(expectedMessage: any)
: This expects a function to throw an error with the given error text when executedtoMatchPattern(expectedMessage: any)
: This expects a string to match the given regular expressionAll the preceding matcher functions resolve to a Boolean value. Sometimes, we will want to evaluate in our assertion the opposite of the comparison actually implemented in the matcher function. For those cases we just need to prepend a .not
modifier before the matcher:
expect(true).not.toBe(false);