Search code examples
javascriptangularunit-testingtestingangular-test

What contents "errorMessage()" function in Angular testing docs. (see the description)


I'm unable to find any source or explanations about errorMessage() function which written here:

https://angular.io/guide/testing#async-test-with-fakeasync.

Can you help me to sort out, what it contains?


Solution

  • The code for errorMessage() function is:

    // Helper function to get the error message element value
    // An *ngIf keeps it out of the DOM until there is an error
    const errorMessage = () => {
      const el = fixture.nativeElement.querySelector('.error');
      return el ? el.textContent : null;
    };
    

    To find this code:

    1- Download example for testing code from here:

    https://angular.io/guide/testing#async-test-with-fakeasync

    So, in the top of the page click on download example

    Direct link: https://angular.io/generated/zips/testing/testing.zip

    2- unzip the file testing.zip and go to this directory: \testing\src\app\twain

    3- Open the file twain.component.spec

    So you will find the helper function errorMessage() in the top of the file and even the test that the link you given in the question refers to.