...

/

Test Setup and Teardown

Test Setup and Teardown

Learn how to initialize and clean up code before and after running tests in TypeScript.

Before we run a particular test, we may wish to exercise some code beforehand. This may be to initialize a particular variable or to make sure that the dependencies of an object have been set up.

In the same vein, we may wish to execute some code once a particular test has run or even after a full test suite has run. To illustrate this, consider the following class:

Press + to interact
class GlobalCounter {
count: number = 0;
increment(): void {
this.count++;
}
}

Here, we have a class named GlobalCounter that has a count property and an increment function.

The count property is set to 0 when the class is instantiated, and the ...