<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Míriam González</title>
    <description>The latest articles on Forem by Míriam González (@miriamgonp).</description>
    <link>https://forem.com/miriamgonp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F434145%2F6c6c946d-358c-4a68-b856-a2105c1a3190.png</url>
      <title>Forem: Míriam González</title>
      <link>https://forem.com/miriamgonp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/miriamgonp"/>
    <language>en</language>
    <item>
      <title>The Importance of end-to-end Testing in Web Development: A Comprehensive Guide 🧪💻</title>
      <dc:creator>Míriam González</dc:creator>
      <pubDate>Mon, 23 Jan 2023 15:40:07 +0000</pubDate>
      <link>https://forem.com/miriamgonp/the-importance-of-end-to-end-testing-in-web-development-a-comprehensive-guide-1in7</link>
      <guid>https://forem.com/miriamgonp/the-importance-of-end-to-end-testing-in-web-development-a-comprehensive-guide-1in7</guid>
      <description>&lt;p&gt;When developing web applications, an important part is to &lt;strong&gt;verify that everything works correctly for all possible use cases&lt;/strong&gt;. This can be done manually for simple applications, but for large applications with a lot of business logic, it is necessary to automate this task. &lt;/p&gt;

&lt;p&gt;That's why &lt;strong&gt;testing is very significant in front-end development&lt;/strong&gt;, and there are several types: unit, integration, and &lt;strong&gt;end-to-end (E2E)&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;When to Introduce Testing in a Project?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It's crucial to understand that the &lt;strong&gt;earlier you begin testing&lt;/strong&gt;, the better. It should be an integral part of the Front-end team's task definition. &lt;/p&gt;

&lt;p&gt;Implementing techniques such as &lt;strong&gt;TDD (Test Driven Development)&lt;/strong&gt; is even better, as defining tests before developing the functionality forces you to anticipate any doubts and makes it easier to find solutions for any issues that may arise.&lt;/p&gt;

&lt;p&gt;From my experience, there have been instances where we were &lt;strong&gt;short on time and delivered functionality without testing 💥&lt;/strong&gt;. This not only creates uncertainty about whether any modifications might break other parts of the code, but also results in &lt;strong&gt;costly tests done retrospectively&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Benefits of end-to-end Testing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;E2E testing for web applications refers to simulating the behavior of a real user interacting with the application through a web browser. This helps to ensure that the application works as expected from the user's perspective.&lt;/p&gt;

&lt;p&gt;The benefits of E2E testing include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;💻 &lt;strong&gt;Complete application overview.&lt;/strong&gt; Allows for simulation of a real user's behavior, providing a complete understanding of the application and ensuring that all components are working correctly together and meeting user expectations.&lt;/li&gt;
&lt;li&gt;🕵️‍♀️ &lt;strong&gt;User interface error detection.&lt;/strong&gt;  Helps to ensure the user interface of the application is functioning correctly and that there are no errors that could negatively impact the user experience.&lt;/li&gt;
&lt;li&gt;🔎 &lt;strong&gt;Integration error detection.&lt;/strong&gt; It helps confirm that different components of the system are properly integrated and that there are no integration errors that could affect the application's performance.&lt;/li&gt;
&lt;li&gt;💰 &lt;strong&gt;Cost-effective in the long term.&lt;/strong&gt;  Although it may be more time-consuming to maintain and slower than other types of testing, E2E testing detects errors early in the development process, allowing for efficient and cost-effective corrections.&lt;/li&gt;
&lt;li&gt;🚀 &lt;strong&gt;Continuous improvement.&lt;/strong&gt; By implementing E2E testing from the beginning, we can ensure that our application is functioning correctly and meeting user expectations, allowing for future improvements and code refactoring.&lt;/li&gt;
&lt;li&gt;📝 &lt;strong&gt;Documentation.&lt;/strong&gt; End-to-end tests serve as valuable documentation throughout the development process, especially when requirements change or certain behaviors of the application need to be remembered.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;E2E testing&lt;/strong&gt; is a crucial tool for ensuring that an application is functioning correctly from the user's perspective and that all components are properly integrated. Despite being slower and more costly to maintain than other types of testing, the benefits outweigh the drawbacks, and it helps prevent &lt;strong&gt;major issues in the future&lt;/strong&gt;🛡.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How to Begin Creating E2E Tests&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Cypress is a popular framework for conducting E2E testing in JavaScript. It is &lt;strong&gt;easy to install, configure&lt;/strong&gt;, and offers an &lt;strong&gt;intuitive interface&lt;/strong&gt; with many features. &lt;/p&gt;

&lt;p&gt;Cypress &lt;strong&gt;runs directly in the browser&lt;/strong&gt;, eliminating the need for configuring testing environments or running tests on different browsers and platforms.&lt;/p&gt;

&lt;p&gt;To start using Cypress, it must first be installed as a &lt;strong&gt;development dependency&lt;/strong&gt; in your project. Then, the Cypress interface can be opened with a specific command.&lt;/p&gt;

&lt;p&gt;For instance, to install Cypress in a Nuxt project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install Cypress as a development dependency in your Nuxt project:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev cypress
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add a Cypress execution script in your package.json file, for example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "cypress:open": "cypress open"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start your project in development mode using the command &lt;code&gt;npm run dev&lt;/code&gt; or &lt;code&gt;yarn dev&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the command &lt;code&gt;npm run cypress:open&lt;/code&gt; to open the Cypress interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Launching Cypress for the first time, you will be guided through a wizard that will create a &lt;code&gt;cypress.config.js&lt;/code&gt; for you.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: '&amp;lt;http://localhost:1234&amp;gt;',
  },
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Structure of an end-to-end test in Cypress&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once you have installed Cypress as a development dependency in your project, you can create a &lt;code&gt;cypress&lt;/code&gt; folder at the root, and inside it, an &lt;code&gt;integration&lt;/code&gt; folder to store your Cypress tests.&lt;/p&gt;

&lt;p&gt;Each Cypress test file should have the &lt;code&gt;.spec.js&lt;/code&gt; extension and can contain multiple test functions. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('My App', () =&amp;gt; {
    it('Visits the app root url', () =&amp;gt; {
        cy.visit('/');
        cy.contains('h1', 'Welcome to My App');
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;describe():&lt;/strong&gt; It's a function that groups multiple &lt;code&gt;it()&lt;/code&gt; related to each other under the same title or description. In the example above, it's used to describe &lt;code&gt;My App&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;it():&lt;/strong&gt; It's a function that contains a specific use case. In the example above, it's used to visit the root URL of our app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;cy object:&lt;/strong&gt; Cypress that provides a variety of commands to interact with the application. In the example above, the visit() command is used to simulate a visit to a specific URL, and &lt;code&gt;contains()&lt;/code&gt; is used to check if a specific element contains a specific text. In the example above, we check if the page contains a h1 tag with the text &lt;code&gt;Welcome to My App&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the advantages of Cypress is that it provides us with a lot of functions to interact with the application. You can check all of them in its &lt;a href="https://docs.cypress.io/api/commands/and"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's important to mention that Cypress allows us to create &lt;strong&gt;asynchronous tests&lt;/strong&gt; using the &lt;code&gt;cy.wait()&lt;/code&gt; function. This is useful when we need to give our application time to perform an action before continuing with the flow of the test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To run the tests&lt;/strong&gt;, you can use the command &lt;code&gt;npm run cypress:run&lt;/code&gt; in the command line, or you can open the Cypress interface with the command &lt;code&gt;npm run cypress:open&lt;/code&gt; and run the tests from there.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Keep in mind that Cypress requires the application to be running on a local server before running the tests&lt;/strong&gt;, thus, verify that your project is running before executing Cypress.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;My Experience in a Real Project 💼&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://www.64robots.com/"&gt;64 Robots&lt;/a&gt;, we decided to implement E2E testing at the start of a project. It was costly and required a learning and setup process at first, but we were able to deliver the first version of the product with a very low error rate. Being able to detect errors before they reach production is a great way to address them in a timely manner. Additionally, it kept the client satisfied as we delivered a stable and robust product.&lt;/p&gt;

&lt;p&gt;Here are some things I've learned during my experience with testing&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🧪 The &lt;strong&gt;quality of the tests&lt;/strong&gt; is crucial in order to maintain trust in their results.&lt;/li&gt;
&lt;li&gt;📝 Starting with a &lt;strong&gt;user story&lt;/strong&gt; can be a helpful approach when writing a test.&lt;/li&gt;
&lt;li&gt;🛠️ Cypress advises &lt;a href="https://docs.cypress.io/guides/core-concepts/conditional-testing#docusaurus_skipToContent_fallback"&gt;against the use of conditionals&lt;/a&gt;, if you find yourself needing them, it's worth re-evaluating the test's behavior.&lt;/li&gt;
&lt;li&gt;💻 Planning the &lt;strong&gt;structure of E2E tests&lt;/strong&gt; during the feature development stage is effective as the business logic is still fresh in mind, which will save time in the long run.&lt;/li&gt;
&lt;li&gt;🔎 Having dedicated &lt;strong&gt;test data&lt;/strong&gt; can aid in covering all possible use cases and provide a solid foundation to work with.&lt;/li&gt;
&lt;li&gt;📚🚀 Starting with E2E testing can be challenging, but with &lt;strong&gt;time and practice&lt;/strong&gt;, the quality of your tests will improve.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In conclusion, while E2E testing can be more time-consuming in the short-term, it offers &lt;strong&gt;long-term benefits to your project&lt;/strong&gt; that make it worth the time invested. &lt;strong&gt;Cypress is a valuable tool&lt;/strong&gt; for E2E testing, with its intuitive interface and ample functionalities, it makes writing simple and complex tests quick and efficient.&lt;/p&gt;

&lt;p&gt;I hope this article has helped you understand the importance of including E2E testing in your projects. If you have any questions or need more information, feel free to leave a comment 💬. Good luck with your tests! 🍀&lt;/p&gt;

</description>
      <category>testing</category>
      <category>webdev</category>
      <category>cypress</category>
      <category>e2e</category>
    </item>
  </channel>
</rss>
