Extra module 6 - Automated Testing

A more robust way of ensuring the system is working as expected.

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 manually anymore, but 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.

Some context about testing is given but, ideally, a basic background of this theme is recommended. This course 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:

yarn test

Some utilities:

  • Tests can be executed in watch mode (similar to hot reload) with test:watch

  • A single test can be executed by putting -- filename at the end

And the e2e tests with:

yarn test:e2e

Let's then focus on tests for the users resource. Beginning by running the test for the UsersService.

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.

Last updated