This website uses cookies to better the user experience of its visitors. Where applicable, this website uses a cookie control system, allowing users to allow or disallow the use of cookies on their computer/device on their first visit to the website. This complies with recent legislative requirements for websites to obtain explicit consent from users before leaving behind or reading files such as cookies on a user’s computer/device. To learn more click Cookie Policy.

Privacy preference center

Cookies are small files saved to a user’s computer/device hard drive that track, save, and store information about the user’s interactions and website use. They allow a website, through its server, to provide users with a tailored experience within the site. Users are advised to take necessary steps within their web browser security settings to block all cookies from this website and its external serving vendors if they wish to deny the use and saving of cookies from this website to their computer’s/device’s hard drive. To learn more click Cookie Policy.

Manage consent preferences

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
Cookies list
Name _rg_session
Provider rubygarage.org
Retention period 2 days
Type First party
Category Necessary
Description The website session cookie is set by the server to maintain the user's session state across different pages of the website. This cookie is essential for functionalities such as login persistence, ensuring a seamless and consistent user experience. The session cookie does not store personal data and is typically deleted when the browser is closed, enhancing privacy and security.
Name m
Provider m.stripe.com
Retention period 1 year 1 month
Type Third party
Category Necessary
Description The m cookie is set by Stripe and is used to help assess the risk associated with attempted transactions on the website. This cookie plays a critical role in fraud detection by identifying and analyzing patterns of behavior to distinguish between legitimate users and potentially fraudulent activity. It enhances the security of online transactions, ensuring that only authorized payments are processed while minimizing the risk of fraud.
Name __cf_bm
Provider .pipedrive.com
Retention period 1 hour
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to support Cloudflare Bot Management. This cookie helps to identify and filter requests from bots, enhancing the security and performance of the website. By distinguishing between legitimate users and automated traffic, it ensures that the site remains protected from malicious bots and potential attacks. This functionality is crucial for maintaining the integrity and reliability of the site's operations.
Name _GRECAPTCHA
Provider .recaptcha.net
Retention period 6 months
Type Third party
Category Necessary
Description The _GRECAPTCHA cookie is set by Google reCAPTCHA to ensure that interactions with the website are from legitimate human users and not automated bots. This cookie helps protect forms, login pages, and other interactive elements from spam and abuse by analyzing user behavior. It is essential for the proper functioning of reCAPTCHA, providing a critical layer of security to maintain the integrity and reliability of the site's interactive features.
Name __cf_bm
Provider .calendly.com
Retention period 30 minutes
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to distinguish between humans and bots. This cookie is beneficial for the website as it helps in making valid reports on the use of the website. By identifying and managing automated traffic, it ensures that analytics and performance metrics accurately reflect human user interactions, thereby enhancing site security and performance.
Name __cfruid
Provider .calendly.com
Retention period During session
Type Third party
Category Necessary
Description The __cfruid cookie is associated with websites using Cloudflare services. This cookie is used to identify trusted web traffic and enhance security. It helps Cloudflare manage and filter legitimate traffic from potentially harmful requests, thereby protecting the website from malicious activities such as DDoS attacks and ensuring reliable performance for genuine users.
Name OptanonConsent
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonConsent cookie determines whether the visitor has accepted the cookie consent box, ensuring that the consent box will not be presented again upon re-entry to the site. This cookie helps maintain the user's consent preferences and compliance with privacy regulations by storing information about the categories of cookies the user has consented to and preventing unnecessary repetition of consent requests.
Name OptanonAlertBoxClosed
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonAlertBoxClosed cookie is set after visitors have seen a cookie information notice and, in some cases, only when they actively close the notice. It ensures that the cookie consent message is not shown again to the user, enhancing the user experience by preventing repetitive notifications. This cookie helps manage user preferences and ensures compliance with privacy regulations by recording when the notice has been acknowledged.
Name referrer_user_id
Provider .calendly.com
Retention period 14 days
Type Third party
Category Necessary
Description The referrer_user_id cookie is set by Calendly to support the booking functionality on the website. This cookie helps track the source of referrals to the booking page, enabling Calendly to attribute bookings accurately and enhance the user experience by streamlining the scheduling process. It assists in managing user sessions and preferences during the booking workflow, ensuring efficient and reliable operation.
Name _calendly_session
Provider .calendly.com
Retention period 21 days
Type Third party
Category Necessary
Description The _calendly_session cookie is set by Calendly, a meeting scheduling tool, to enable the meeting scheduler to function within the website. This cookie facilitates the scheduling process by maintaining session information, allowing visitors to book meetings and add events to their calendars seamlessly. It ensures that the scheduling workflow operates smoothly, providing a consistent and reliable user experience.
Name _gat_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _gat_UA-* cookie is a pattern type cookie set by Google Analytics, where the pattern element in the name contains the unique identity number of the Google Analytics account or website it relates to. This cookie is a variation of the _gat cookie and is used to throttle the request rate, limiting the amount of data collected by Google Analytics on high traffic websites. It helps manage the volume of data recorded, ensuring efficient performance and accurate analytics reporting.
Name _ga
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga cookie is set by Google Analytics to calculate visitor, session, and campaign data for the site's analytics reports. It helps track how users interact with the website, providing insights into site usage and performance.
Name _ga_*
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga_* cookie is set by Google Analytics to store and count page views on the website. This cookie helps track the number of visits and interactions with the website, providing valuable data for performance and user behavior analysis. It belongs to the analytics category and plays a crucial role in generating detailed usage reports for site optimization.
Name _gid
Provider rubygarage.org
Retention period 1 day
Type First party
Category Analytics
Description The _gid cookie is set by Google Analytics to store information about how visitors use a website and to create an analytics report on the website's performance. This cookie collects data on visitor behavior, including pages visited, duration of the visit, and interactions with the website, helping site owners understand and improve user experience. It is part of the analytics category and typically expires after 24 hours.
Name _dc_gtm_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _dc_gtm_UA-* cookie is set by Google Analytics to help load the Google Analytics script tag via Google Tag Manager. This cookie facilitates the efficient loading of analytics tools, ensuring that data on user behavior and website performance is accurately collected and reported. It is categorized under analytics and assists in the seamless integration and functioning of Google Analytics on the website.

Why Does My React App Need TDD?

  • 18073 views
  • 9 min
  • Apr 09, 2020
Mykola V.

Mykola V.

IOS/Android Developer

Daria R.

Daria R.

Copywriter

Share

When starting a software development project, it’s essential to choose the most effective approach to save time, meet the budget, and get a product that works perfectly. 

Test-driven development (TDD) is one approach you can choose for React app development. What are the benefits and drawbacks of TDD? Is it worth your time and money? What are the best tools to set up TDD on your project? Answers to these and many other questions are in this post. 

What’s test-driven development?

Test-driven development is an approach when developers create a product backwards. With a traditional development approach, developers write code first and then test it. TDD requires developers to write tests first and only then start to write the code. 

The easiest way to explain how TDD works is to imagine a three-step process: Red, Green, Refactoring. 

Red

The first step starts with developers describing the work scenario in a test. Then they create the objects required so the code compiles, run the test, and see a red bar because the test fails.

Green

Now developers need to write code that can pass the test. This code doesn’t have to be perfect. The only criterion is its ability to pass the test.

Refactor

Once the code passes the test, developers can refactor it. After refactoring, developers run the test one more time to make sure they didn’t break anything during refactoring.


tdd in react
Three key processes of TDD

Benefits of test-driven development for React apps

Test-driven development may be a new approach for some teams, and it may become an obstacle for them. Nevertheless, TDD has a long list of benefits. Let’s take a look.

High quality of code

When following a test-driven development approach, developers have to write tests before the code itself. As a result, the code that developers create has very high test coverage, meaning all bugs, imperfections, and flaws are easy to detect and fix. 

Another aspect of TDD that influences code quality is the essential step of refactoring. This is the process of improving existing code to make it more structured, organized, and easy to understand.

Cut costs

Test-driven development helps to reduce the costs of React app development by influencing the following:

  • Scope of work. One of the most common reasons for a constantly growing scope of work is a lack of proper documentation. It’s difficult for developers to estimate the real scope of a task if they don’t know all the requirements. TDD solves this problem because the development process starts with creating unit tests, which are commonly used as documentation. These tests describe what the code has to do, so developers know what they need to implement and how much time they need to do so. 
  • Quality of code. High-quality code not only works seamlessly and leads to a bug-free product. It also saves money on code support and maintenance. If your code has 100% coverage, you can easily add new functionality without breaking things. 
  • Amount of rework. When using a common linear workflow (code first, test second), developers can write code without having all the requirements. When following TDD, developers have to clarify all technical and business requirements in order to create tests. These tests allow team members to get a uniform vision of the project and deliver on it, avoiding misunderstandings and rework.

Simple debugging

Test-driven development promotes good coding principles such as Don’t Repeat Yourself (DRY), Keep It Simple, Stupid! (KISS), You Ain’t Gonna Need It (YAGNI), and many others. 

These principles encourage developers to write clean, well-organized, and easy to read code. It’s much harder to debug messy, overly complicated code than code with a clear structure. 

Additionally, high-quality code is easy to work on even if you change your development team or new specialists join the project. 

Detailed product documentation 

Tests that developers write before coding can be used as detailed documentation that explains how the code works and what it does. Documentation is particularly important for the success of your project in the following cases:

  • Migrating to new technologies. You may decide to migrate your product to another technology stack when you need newer, more powerful libraries, frameworks, and even languages. When migrating to new technologies, developers use documentation to make sure they clearly understand how the code works.
  • Changing a contractor. If you decide to change your software development company, your new contractor is likely to start with a code review to get familiar with your product’s codebase. Documentation is vital to helping your new team understand all parts of your product, maintain it, and modify it when necessary.
  • Product development. The reason why a lot of entrepreneurs start their products with a minimum viable product (MVP) is that it offers a chance to test their business idea and then modify it to meet customers’ needs. Documentation allows developers to add functionality and modify the system without spending too much time getting familiar with it. 

Ensures better code structure

Unit tests can only work with code that has a modular structure. A modular code structure helps to avoid unnecessary dependencies, as each part of the code can be modified with little or no influence on other parts. 

Besides, when writing tests first, developers have to think in advance how the code will function and can detect potential bottlenecks at this stage. 

Drawbacks of test-driven development for React apps 

Despite all the powerful benefits we’ve described, TDD also has drawbacks that can persuade you not to follow this approach. Let’s consider them to find out how we can overcome them.

Test-driven development takes longer

Instead of writing code straightaway, developers following the TDD approach need to think through the code architecture first, then create tests, and only after that start to code. 

As a result, you get code later than when you follow a traditional development approach. However, you need to take into account that the code you get when following TDD is cleaner, does exactly what it’s supposed to, and has significantly fewer bugs. 

In the long term, code written with the TDD approach saves you time and money on maintenance, support, and further development. 

Doesn't help with projects that lack sufficient preparation 

TDD is only useful if developers know the purpose of code. To create a unit test, developers need to understand how code will function and what it will do. And for that, developers need to know all business and technical requirements.

This means that before test-driven development starts, you (or your contractors) need to collect the requirements for your project, prepare specifications, create a feature breakdown list, and compile many other materials that will be used by developers, quality assurance engineers, designers, and other specialists.

Need to maintain tests as well as code

Tests are a huge part of your product. When you make changes to the codebase, you have to change tests as well. 

When applying TDD, if you need to make a change, developers first need to modify tests and only then can modify the code. 

TDD is difficult for beginners

Young development teams may face challenges with TDD. It takes time and effort to learn to write laconic unit tests for code that doesn’t even exist yet. Besides, some inexperienced developers may believe tests aren’t that necessary and refuse to spend time on them.

However, more experienced companies are already aware of the benefits of unit tests and TDD and practice it daily.

The drawbacks can be avoided

All the drawbacks we’ve described above aren’t so difficult to overcome. Besides, the result is worth the effort. 

If you agree, we recommend you take a look at the tools RubyGarage uses to implement test-driven development on React projects.

Best tools to support TDD for React development

In this section, we’re going to tell you about the tools our team uses for TDD on our React projects. We’re also going to share how we use them and why we prefer them to other tools.

Jest

Jest is a JavaScript testing framework that’s easy to use. It has a complete out-of-the-box set of tools and allows for parallelizing test performance. 

The RubyGarage team uses Jest for unit and integration testing. With Jest, we cover business logic in components, everything related to APIs, and application states with tests. 

Although there are some great alternatives to Jest, like Jasmine and Mocha, we prefer Jest because of the following benefits:

  • Zero configuration. Jest has everything you need for almost any JavaScript project out of the box. This means developers can spend minimum time configuring the system. 
  • Snapshots. Snapshots allow us to easily keep track of large objects. This is handy if you want to be sure the user interface doesn’t change unexpectedly. Snapshots can live alongside your tests or be embedded inline.
  • Isolation. With Jest, developers can run tests in parallel. This significantly improves testing performance.
  • Proper documentation. Jest is a well-organized instrument with clear, up-to-date documentation.

Enzyme

Enzyme, created by Airbnb, is a library that simplifies testing of React apps by offering convenient functions for rendering components.Enzyme allows you to perform full rendering, simplified rendering, and static rendering. 

Our team uses Enzyme to cover all React components with snapshot tests. We chose this tool because of its shallow rendering feature. Shallow rendering allows us to get a snapshot of a component, abstracting from the implementation of its child components. 

If you’re looking for an alternative to Enzyme, we recommend you try React-testing-library, as it also has a range of powerful features for testing components.

Cypress

Cypress is a testing tool that helps developers and quality assurance engineers check how the client side of an app interacts with the back end and how different page components interact with each other. 

In our team, we use Cypress to write end-to-end tests. For instance, if we need to test user login, we launch a browser, open the URL we need, enter user data, send the form, and check if the site throws us to a dashboard. Cypress tests run in a browser automatically. 

Among the benefits of Cypress are:

  • Ability to check test performance as test runs with the help of snapshots. 
  • Synchronous test performance. Cypress automatically waits for commands and assertions so developers don’t have to add waits or sleeps manually.
  • Extended functionality. Cypress allows developers to control the behavior of functions, server responses, and timers, just like in unit tests.

If you don’t want to use Cypress, you can use Selenium or Capybara. 

Wrapping up

The TDD approach has already shown an undeniable positive effect on React web app development. Our team has felt this influence and recommends the TDD approach for startups that want to create a high-quality product in the shortest time possible.

CONTENTS

FAQ

    • High quality of code
    • Lower costs
    • Simple debugging
    • Detailed product documentation 
    • Better app architecture
    • Test-driven development takes longer
    • TDD can’t help on a project that has insufficient preparation 
    • Developers need to maintain tests as well as code
    • TDD is difficult for beginners
  1. The RubyGarage team has extensive experience providing React JS development services. We have successfully launched dozens of React projects where we’ve applied the TDD approach. Based on our experience, we recommend the following tools to set up test-driven development for your project:

    • Jest
    • Enzyme
    • Cypress

Authors:

Mykola V.

Mykola V.

IOS/Android Developer

Daria R.

Daria R.

Copywriter

Rate this article!

Nay
So-so
Not bad
Good
Wow
10 rating, average 4.8 out of 5

Share article with

Comments (0)

There are no comments yet

Leave a comment

Subscribe via email and know it all first!