Setup and Teardown of Tests

Let's explore how we can use the setup and teardown function to initialize something within the test spec.

Teardown functions

When writing tests, sometimes we need to set up some things before running a test spec.

The type of things we may need to initialize before running a test can include:

  • Creating mock data
  • Setting some global variables
  • Creating mock services needed to run the function under test.

All this can be handled as part of the setup of a test, but we can also clear these mock services and data as part of the teardown of a test suite. To do this, Jasmine provides us with four functions we can use in a test suite, which are set out in the following list:

Get hands-on with 1200+ tech skills courses.