Skip to main content
Complete Guide to Unit Testing in React with TypeScript: Enhance the Quality of Your Frontend | Kranio

Keep in mind that this configuration is a basic setup. It can be customized according to the specific needs of the project.

You should integrate the Jest configuration into the package.json:

__CODE_BLOCK_PLACEHOLDER_2__

"test": "jest --watch": Runs tests while you work and shows results instantly.

"test:coverage": "jest --coverage": can help you identify which parts of your code need more tests.

Once installed, to run the tests we use the following command; it can run all tests or you can specify the filename to test them one by one.

Jest will automatically look for files matching the pattern *.test.tsx and run the tests defined in them; in this case, it is .tsx because we are using TypeScript.

__CODE_BLOCK_PLACEHOLDER_3__

Jest will automatically look for files matching the pattern *.test.tsx and run the tests defined in them; in this case, it is .tsx because we are using TypeScript.

__CODE_BLOCK_PLACEHOLDER_4__

Comparison Methods in Jest and React Testing Library

When writing tests for our React code using Jest and React Testing Library, it's important to know how to compare expected results with the values obtained during tests. In this document, we will see how to make these comparisons effectively.

Types of queries in Testing library (screen)

__CODE_BLOCK_PLACEHOLDER_5__
__CODE_BLOCK_PLACEHOLDER_6__
__CODE_BLOCK_PLACEHOLDER_7__

Main queries

  1. getByRole: This query can be used to find all elements exposed in the accessibility tree. With the name option, you can filter the returned elements by their accessible name. It should be your primary preference for almost everything.
  2. getByLabelText: Very useful for form fields. Users navigate forms using labels. This query emulates that behavior and should be your first choice for form fields.
  3. getByPlaceholderText: A placeholder does not replace a label, but if that's all you have, it's better than other alternatives.
  4. getByText: Outside forms, text content is the main way users find elements. It can be used to find non-interactive elements like divs, spans, and paragraphs.
  5. getByDisplayValue: The current value of a form element can be useful when navigating a page with pre-filled values.

Semantic Queries

  1. getByAltText: If the element supports alternative text (img, area, input, and any custom element), this can be used to find that element.
  2. getByTitle: The title attribute is not consistently read by screen readers and is not visible by default to sighted users.

Test IDs

  1. getByTestId: Users cannot see (or hear) them, so it is only recommended in cases where you cannot match by role or text, or it doesn't make sense (for example, the text is dynamic).
đź’ˇTo better understand these concepts, it is recommended to review the official documentation and watch a video that, in my opinion, was very helpful for understanding unit tests. https://testing-library.com/docs/queries/about/

How to know when a test is async?

In JavaScript, and in the context of unit testing such as Jest and React Testing Library, a test is considered asynchronous (async) when it involves operations that do not execute immediately, such as calls to functions that return Promises, timers (e.g., setTimeout), or interactions with asynchronous APIs (like network requests).

Synchronous Test (Non-Async):

__CODE_BLOCK_PLACEHOLDER_8__

Asynchronous Test (Async):

__CODE_BLOCK_PLACEHOLDER_9__

Creating Tests

__CODE_BLOCK_PLACEHOLDER_10__

In this example, we are using the render and screen.getByText functions from React Testing Library to render the Button component and check if the button element with the text "Click me" is present in the document.

đź’ˇ jest.fn(): It is a tool that allows creating mock functions that keep track of their calls during tests. This is useful to verify if a function is called correctly or to imitate the behavior of functions in test situations.

__CODE_BLOCK_PLACEHOLDER_11__

Suppose we need to test whether there is a title in the EventExceptionForm component; the test function can be started with test or it.

__CODE_BLOCK_PLACEHOLDER_12__

Then, multiple expect assertions are used to verify that the handlers have been called. This is done with the following lines:

‍

__CODE_BLOCK_PLACEHOLDER_13__

‍

__CODE_BLOCK_PLACEHOLDER_14__

The expression uses multiple ternary operators (? :) to determine the value of **buttonText** based on the container's state (container.currentState).

This code performs a test to confirm that the button displayed in the user interface matches the current state of a container. It ensures that the interface shows the appropriate button for the container's state.

Conclusion

Unit tests are an essential part of the software development process. They allow us to identify and fix issues in our components and functions before they reach production. By using Jest and React Testing Library, I can create effective unit tests for my React applications. I understand that writing solid tests not only improves the quality of my code but also facilitates the maintenance and evolution of my project.

References

‍https://medium.com/@angelygranados/cómo-empezar-a-hacer-unit-testing-con-jest-guía-básica-ca6d9654672

‍https://www.youtube.com/watch?v=QdqIqGPsLW0

‍https://www.youtube.com/watch?v=bTGil8qPmXo

Ready to improve the quality and reliability of your frontend with effective unit tests?

At Kranio, we have testing experts who will help you implement unit testing strategies using tools like Jest and React Testing Library, ensuring your React applications work correctly and provide an excellent user experience. Contact us and discover how we can help you elevate the quality of your frontend software.

: 'ts-jest',  }, }; export default config;

It should be noted that this configuration is a basic configuration. It can be customized according to the specific needs of the project.

The configuration for jest should be integrated into the package.json:

__CODE_BLOCK_PLACEHOLDER_2__

"test": "jest --watch": Runs tests while you work and shows you results instantly.

"test:coverage": "jest --coverage": can help you identify which parts of your code need more testing.

Once it is installed, to run the test tests we use the following command, it can be alone and it will run all the tests but you can also specify the name of the file you want to run to test them one by one

Jest will automatically look for files matching the pattern *.test.tsx and run the tests defined in them, in this case it is .tsx because we are using typescript.

__CODE_BLOCK_PLACEHOLDER_3__

Jest will automatically look for files matching the pattern *.test.tsx and run the tests defined in them, in this case it is .tsx because we are using typescript.

__CODE_BLOCK_PLACEHOLDER_4__

Comparison Methods in Jest and React Testing Library

When writing tests for our React code using Jest and React Testing Library, it is important to know how to compare expected results with the values obtained during testing. In this document, we will see how to make these comparisons effectively.

Types of queries in Testing library (screen)

__CODE_BLOCK_PLACEHOLDER_5__
__CODE_BLOCK_PLACEHOLDER_6__
__CODE_BLOCK_PLACEHOLDER_7__

Main queries

  1. getByRole: This query can be used to find all elements that are exposed in the accessibility tree. With the name option, you can filter the returned elements by their accessible name. It should be your primary preference for almost everything.
  2. getByLabelText: It is very useful for form fields. Users navigate forms using labels. This query emulates that behavior and should be your first choice for form fields.
  3. getByPlaceholderText: A placeholder does not replace a label, but if that is all you have, it is better than other alternatives.
  4. getByText: Outside of forms, text content is the main way users find elements. It can be used to find non-interactive elements like divs, spans, and paragraphs.
  5. getByDisplayValue: The current value of a form element can be useful when navigating a page with pre-filled values.

Semantic Queries

  1. getByAltText: If the element is one that supports alternative text (img, area, input, and any custom element), this can be used to find that element.
  2. getByTitle: The title attribute is not consistently read by screen readers and is not visible by default to users with sight.

Test IDs

  1. getByTestId: The user cannot see them (nor hear them), so it is only recommended in cases where you cannot match by role or text, or it does not make sense (for example, the text is dynamic).
đź’ˇTo better understand these concepts, it is recommended to review the official documentation and watch a video that, in my opinion, was very helpful for understanding unit testing. https://testing-library.com/docs/queries/about/

How to know when a test is async?

In JavaScript, and in the context of unit testing, such as Jest and React Testing Library, a test is considered asynchronous (async) when it involves operations that do not execute immediately, such as calls to functions that return Promises, timers (e.g., setTimeout), or interactions with asynchronous APIs (like network requests).

Synchronous Test (Non-Async):

__CODE_BLOCK_PLACEHOLDER_8__

Asynchronous Test (Async):

__CODE_BLOCK_PLACEHOLDER_9__

Creating Tests

__CODE_BLOCK_PLACEHOLDER_10__

In this example, we are using the render and screen.getByText functions from React Testing Library to render the Button component and verify if the button element with the text "Click me" is present in the document.

đź’ˇ jest.fn(): It is a tool that allows you to create mock functions that keep track of their calls during tests. This is useful to verify if a function is called correctly or to imitate the behavior of functions in test situations.

__CODE_BLOCK_PLACEHOLDER_11__

Suppose we need to test if there is a title or not in the EventExceptionForm component, the test function can be started with test or it.

__CODE_BLOCK_PLACEHOLDER_12__

Then, multiple expect assertions are used to verify that the handlers have been called. This is done with the following lines:

‍

__CODE_BLOCK_PLACEHOLDER_13__

‍

__CODE_BLOCK_PLACEHOLDER_14__

The expression uses multiple ternary operators (? :) to determine the value of **buttonText** based on the container's state (container.currentState).

This code performs a test to confirm that the button shown in the user interface matches the current state of a container. It ensures that the interface displays the appropriate button for the container's state.

Conclusion

Unit tests are an essential part of the software development process. They allow us to identify and fix issues in our components and functions before they reach production. By using Jest and React Testing Library, I can create effective unit tests for my React applications. I understand that writing solid tests not only improves the quality of my code but also facilitates the maintenance and evolution of my project.

References

‍https://medium.com/@angelygranados/cómo-empezar-a-hacer-unit-testing-con-jest-guía-básica-ca6d9654672

‍https://www.youtube.com/watch?v=QdqIqGPsLW0

‍https://www.youtube.com/watch?v=bTGil8qPmXo

Ready to improve the quality and reliability of your frontend with effective unit tests?

At Kranio, we have testing experts who will help you implement unit testing strategies using tools like Jest and React Testing Library, ensuring that your React applications work correctly and provide an excellent user experience. Contact us and discover how we can help you elevate the quality of your frontend software.

Previous Posts

Augmented Coding vs. Vibe Coding

Augmented Coding vs. Vibe Coding

AI generates functional code but does not guarantee security. Learn to use it wisely to build robust, scalable, and risk-free software.

Kraneating is also about protection: the process behind our ISO 27001 certification

Kraneating is also about protection: the process behind our ISO 27001 certification

At the end of 2025, Kranio achieved ISO 27001 certification after implementing its Information Security Management System (ISMS). This process was not merely a compliance exercise but a strategic decision to strengthen how we design, build, and operate digital systems. In this article, we share the process, the internal changes it entailed, and the impact it has for our clients: greater control, structured risk management, and a stronger foundation to confidently scale systems.