Angular 2 custom matcher functions

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 Promise
  • toBeAnInstanceOf(expected: any): This expects the actual value to be an instance of a class defined in the expected argument
  • toHaveText(expected: any): This expects the element to have exactly the text defined in the expected argument
  • toHaveCssClass(expected: any): This expects the element to have the CSS class defined in the expected argument
  • toHaveCssStyle(expected: any): This expects the element to be styled with the given CSS styles defined in the payload
  • toImplement(expected: any): This expects a class to implement the interface of the given class
  • toContainError(expected: any): This expects an exception to contain an expected error text
  • toThrowErrorWith(expectedMessage: any): This expects a function to throw an error with the given error text when executed
  • toMatchPattern(expectedMessage: any): This expects a string to match the given regular expression

All 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);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset