Logo
EN

Testing a Typescript Library with Jest

Overview

In this article I am going to share in detail all the main concepts to understand testing with jest focused on a vaquean library that can be used for a client or a server.

The project setup can be found in the following and details of how it was created can be found in the following .

Contrary to what you may imagine, testing with jest is not complex, the truth is it is quite simple from its configuration to the execution of different forms of testing, however it is important to understand several aspects to be able to implement them within a project in an effective way.

Although in this article I do not focus on describing each of the functions that can be used, the idea is to conceptually abstract all the tools used within this library so that you have a general and concrete vision of how to implement it and where to go to respond potential doubts.

Jest is a comprehensive testing framework designed primarily for JavaScript applications, particularly those built with React. Its development by Facebook was driven by the need for a robust, efficient, and user-friendly testing solution that could handle the complexities of modern web applications. Below, we explore the core concepts, principles, and elements that define Jest, emphasizing its philosophy and approach to testing.

Core Concepts of Jest

Testing Framework vs. Library

Jest is not a library , it's a testing framework. Since instead of proposing a couple of variable functions, classes and methods that can be used to test one thing or another, it creates a structured framework of how the tests should be carried out in an organized way and also provides important tools to handle different cases or problems

Zero Configuration

Although complex configurations can be added, the default or minimal ones are sufficient to perform testing effectively.

Isolation

One of the strengths of this framework is that the tests are executed in isolated environments, which means that each test does not depend on another, which makes it essential for unit testing since it can test specific blocks of code without affecting other blocks. of code in the project or even other tests

Elements of Jest

This part is crucial because they are the elements that provide specific functionalities with a specific approach to solve certain problems that arise. Understanding this will help us know where we should look and what we should do depending on the problem we are solving.

Test Runner

The Test Runner is the main element within the framework because it is responsible for executing the tests, managing the life cycle, collecting the results and providing feedback. In an automated way, it locates all the files that contain the tests and executes them in parallel, which makes obtaining results quite fast.

And it also allows you to specify configurations such as execution modes, nipple execution specification, filtering criteria for the tests that should be executed based on a pattern, code coverage reports, as well as custom testing environments such as testing for browsers as an environment or not. yes for server-side testing.

We can see it as a Macro configuration that we can make about our tests and in this way adapt it to the required planning and organization.

Mocking

In software testing, mocking refers to the practice of replacing real implementations of functions, modules, or APIs with controlled, simulated versions.

The Test Runner is the main element within the framework because it is responsible for executing the tests, managing the life cycle, collecting the results and providing feedback. In an automated way, it locates all the files that contain the tests and executes them in parallel, which makes obtaining results quite fast.

And it also allows you to specify configurations such as execution modes, nipple execution specification, filtering criteria for the tests that should be executed based on a pattern, code coverage reports, as well as custom testing environments such as testing for browsers as an environment or not. yes for server-side testing.

We can see it as a Macro configuration that we can make about our tests and in this way adapt it to the required planning and organization.

Assertions

At the heart of the assertion library are the assertion functions. These functions allow to make declarative statements about the expected outcome of a test.

The most commonly used assertion function in Jest is expect, which serves as the starting point for creating assertions.

Matchers are the core of the assertion library, providing a wide range of functions for comparing values, checking conditions, and making assertions.

In addition to the built-in matchers, Jest allows developers to create custom matchers to suit specific testing needs.

Code Coverage

Code coverage refers to the percentage of code that is executed during testing. It helps developers understand which parts of their codebase are tested and which are not. Code coverage can be measured in several ways, including:

CLI Tool

The Jest CLI tool allows developers to interact with the Jest testing framework through command-line commands. This interface provides a wide range of functionalities that enable developers to manage their testing process without needing to rely on graphical user interfaces or extensive configuration files.