concept beforeEach in category angular

appears as: beforeEach
Testing Angular Applications

This is an excerpt from Manning's book Testing Angular Applications.

The part of the tests where you set variables like this is known as the setup. In your setup, you’ll use the beforeEach method to initialize your contact variable every time a test runs. Add a new line and the following code directly beneath the contact variable declaration that you previously added:

beforeEach(() => {
  contact = new ContactClass();
});

You’ll use the beforeEach functions to set up your tests and to execute expressions before each one of your tests runs. In this case, you’re setting the contact variable to a new instance of the ContactClass class each time you run your test.

Now you can write your test. If the test creates an instance of ContactClass successfully, it will use the class’s constructor to do so. You’ll test this by seeing if the contact is not null. Add a new line directly below the beforeEach method that you previously added, and then add the following test case: