# Extra module 6 - Automated Testing

It's now time to learn how to implement **automated tests** for our system. These tests are important to make sure that it behaves as expected. For example, if the methods are doing what they should (invoking other methods, throwing the appropriate exceptions, etc). Each test checks for a possible scenario and if the outcome is what it should be.

As a system grows more complex, it becomes very important that its functionalities are not tested only manually anymore, but also automatically. This makes testing **faster**, **easier** and **less error-prone**.

In our context, basically, there are two main kinds of test:

* **Unit test**: verifies the functionality of a single function or class, checking if it does what it should correctly. External dependencies should be **mocked**, in order to maintain **isolation**.
* **e2e test**: verifies a more realistic behavior of the system. Here, flows that are close to what the end user would experience are executed, with many parts of the system integrated.

{% hint style="info" %}
Some context about testing is given but, ideally, a basic background about this theme is recommended.
{% endhint %}

> The course [JavaScript Unit Testing - The Practical Guide](https://www.udemy.com/course/javascript-unit-testing-the-practical-guide) by **Maximilian Schwarzmüller** (Udemy) may be a great study before delving further.

When a schematic is used to create something, a basic test is also generated. We can then run the **unit tests** in our system with the following command:

```sh
yarn test
```

{% hint style="info" %}
Some utilities:

* Tests can be executed in **watch mode** (similar to **hot reload**) with <mark style="color:orange;">test:watch</mark>
* A single test can be executed by putting <mark style="color:orange;">-- filename</mark> at the end
  {% endhint %}

And the **e2e tests** with:

```sh
yarn test:e2e
```

Let's then focus on tests for the **users resource**. Begin by running the test for the <mark style="color:blue;">`UsersService`</mark>.

```sh
yarn test:watch -- users.service
```

But we'll face a problem: the paths of imports cannot be resolved. In the next section, let's properly configure **Jest** before proceeding further.
