<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ishani Maduwanthi on Medium]]></title>
        <description><![CDATA[Stories by Ishani Maduwanthi on Medium]]></description>
        <link>https://medium.com/@ishmadhuwanthi?source=rss-dcf5c4232933------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4DWGwKvFwRS74HicUVQKQg.jpeg</url>
            <title>Stories by Ishani Maduwanthi on Medium</title>
            <link>https://medium.com/@ishmadhuwanthi?source=rss-dcf5c4232933------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:29:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ishmadhuwanthi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unit Testing in React Applications with Cypress]]></title>
            <link>https://medium.com/@ishmadhuwanthi/unit-testing-in-react-applications-with-cypress-2557783f93ac?source=rss-dcf5c4232933------2</link>
            <guid isPermaLink="false">https://medium.com/p/2557783f93ac</guid>
            <dc:creator><![CDATA[Ishani Maduwanthi]]></dc:creator>
            <pubDate>Mon, 28 Oct 2024 13:36:39 GMT</pubDate>
            <atom:updated>2024-10-28T13:36:39.010Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Unit Testing in React Applications with Cypress</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c6OnTobnm6EtWrPLDqHYQw.png" /></figure><p><strong>Unit Testing</strong></p><p>Unit testing is an important part of any software development process. It is usually performed by the developers during the development.</p><p>It allows testing the application as individual testable components, (not the whole application at once) and it helps to identify bugs or any missing scenarios within the development phase before the integration testing. This will improve the code quality and maintainability. After integration is done, QA testers also do unit tests to reduce the production bugs.</p><p>But from this article, I’ll discuss the unit testing done by developers regarding my experiences.</p><p><strong>Main Unit Test Libraries Used by React Developers</strong></p><p>React is one of the most popular front-end JavaScript libraries among developers. Many testing frameworks can be used with React.</p><p>1. Jest</p><p>2. Enzyme</p><p>3. RTL (React Testing Library)</p><p>4. Cypress</p><p>It is easy to write and execute test cases and generate test summary reports including results with those testing frameworks.</p><ol><li><strong>Jest</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*twgnP-FpUV3aEP5XzAsCjA.png" /></figure><p>Jest is an open-source JavaScript testing framework. Jest support for both front-end and back-end testing. Considering the usability of Jest, it is known as a test runner in React’s official documentation because <em>“</em><strong><em>Jest is in the process of testing current test cases”</em></strong><em>,</em> every time the test cases are changed.</p><p>Jest can be easily installed with <strong>npm</strong> or <strong>yarn</strong> and works with projects that use React, Vue, TypeScript, JavaScript, Babel, node, etc.</p><p>If developers want to test with snapshots, jest can create snapshots when running specific test cases. Mocking and assertions are used in test cases. Usually, jest is used to test rendered applications.</p><p><strong>2. Enzyme</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/223/1*YtxfyvmlcriSBhSQzZn59Q.png" /></figure><p>Enzyme is a JavaScript testing library used in React component testing with the capability of assertion, manipulation, and traversing over the components’ output. When using Enzyme, it should provide additional utilities to interact with the elements.</p><p>Enzyme is not much used in functional-based components because it works better with React class-based components.</p><p>So, people are tempted to use Jest and Enzyme together. Then Jest is used as a test runner while Enzyme builds tests for the UI and assertions are written with Jest. This approach makes debugging easy when a test breaks.</p><p>3. <strong>RTL (React testing Library)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*vuY24ImYeAGbjslDBSe_9Q.png" /></figure><p>React Testing Library — RTL provides a very lightweight solution for testing which builds on the top of the “DOM Testing Library”. It focuses on testing components as the end-user’s experience with them.</p><p>The test cases are written by using RTL work with actual DOM nodes. So, they are more effective and reliable than Jest or Enzyme. Because, this approach, tests the actual behavior of components while the Jest and Enzyme deal with instances of rendered components.</p><p>4. <strong>Cypress</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*urxchoeHz_G_hQtgnPtoGg.png" /></figure><p>Cypress is a next-generation automated open-source front-end testing framework developed for modern web applications. It is based on JavaScript which is built with libraries like Mocha and chai. Cypress supports BDD (Behavior Driven Development) and TDD (Test Data Driven) Testing assertion styles. <br> <br>Cypress is a developer-friendly tool that uses a unique DOM manipulation technique and operates directly in the browser making asynchronous testing simple and convenient. So, this is becoming popular among both developers and QA testers.</p><p>Cypress covers two major testing types:</p><p><em>1. End-to-End Testing (E-2-E)</em></p><p><em>2. Unit Testing</em></p><p>Apart from React, Cypress supports other frameworks like Angular, Vue, or React Native.</p><p>Let’s go through Unit testing with Cypress.</p><p><strong>Cypress is better than RTL</strong></p><p>&gt;&gt; As Cypress is designed for both E2E testing and unit testing, according to the requirement we can do E2E testing or unit testing. So, there is no need to follow separate libraries for the two types of testing.</p><p>But in RTL, it is designed to test the application UI and interactions of the components. That means if we need to ensure the components are rendering and behaving correctly, we can use RTL. But for E2E testing, need to go with Cypress or a suitable library.</p><p>&gt;&gt; Cypress can be used to test more advanced scenarios, like interactions with APIs or databases. However, it is more difficult to test with RTL.</p><p>&gt;&gt; Cypress is better for robust testing solutions because it provides more advanced features than RTL.</p><p>Ex: — Visual testing, time-travel debugging, and network stubbing.</p><p>In the case of RTL, it provides lightweight testing solutions. As it can be easy to set up in the testing workflow, if someone wants to do simple test scenarios like validate UI it is better to use RTL.</p><p>&gt;&gt; In Cypress, it creates an end-to-end journey through the application. So that improves the developer’s confidence after deployment.</p><p>&gt;&gt; Compared to the Cypress resultant time, RTL provides its feedback faster than Cypress. But in Cypress, the advantage is that it provides extra tooling like video recordings and screenshots proof for each test step with its test results.</p><p><strong>Benefits of Testing with Cypress</strong></p><p>&gt;&gt; Live code reloading. Cypress tests can be executed in real-time while writing test cases.</p><p>&gt;&gt; Cypress has a browser debugging environment and supports Chrome, Firefox, and Edge browsers.</p><p>&gt;&gt; There are two options to run tests: running headless tests or testing in a browser.</p><p>&gt;&gt; Cypress provides visual feedback while executing.</p><p>&gt;&gt; Ability to capture snapshots when test cases are running. This helps testers to go over the steps and troubleshoot what happened at the relevant step if any test failure occurred.</p><p>&gt;&gt; Ability to save screenshots and video recordings separately for each test case.</p><p>&gt;&gt; Cypress can test over every layer of the application. (UI, database, API…etc.)</p><p><strong>Setup React Project with Cypress</strong></p><p>First, create a react application in the project folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*Qhkmq4OcezONydVovmk8gg.png" /></figure><p>Then install Cypress in the project. For that, inside the application root folder, run the following command and add Cypress as a dev dependency.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*-iAo_DXFs64-66kmI_fodQ.png" /></figure><p>Adjust the “scripts” block in “<em>package.json</em>”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*O_fpsjIQjPTJneOelhCUmw.png" /></figure><p>Run the server locally to configure the cypress in the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*2pcr-61JNVl4RtjjMv1T4Q.png" /></figure><p>This will open a window with two options, E2E Testing and Component Testing. Select the “<strong>Component Testing</strong>” option to do unit testing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*RYgYUlq0K_rzEnWrmYJdLg.png" /><figcaption>Figure 01: Cypress Welcome Page</figcaption></figure><p>Then it will open the configure files page and after continuing, cypress will generate configuration files in the project folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*m1WhSoWXNvg5M_Vf02yRpw.png" /><figcaption>Figure 02: Cypress Configuration Files</figcaption></figure><p>After a while, the browser selection page will open, and should select the preferable browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*4-jwA-lJdznPNxP0m_Lz_w.png" /><figcaption>Figure 03: Browser Selection Page</figcaption></figure><p>Now after configuring files, the following folder structure will be created in the project folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*oZX33Gv8HZCs-rhixxtIPw.png" /><figcaption>Figure 04: Cypress folder structure</figcaption></figure><p><strong>downloads</strong>: Screenshots or video recordings of the test being executed here.</p><p><strong>fixtures</strong>: This will load up all the JSON data needed to do the test.</p><p><strong>screenshots</strong>: Screenshots are stored in this folder by default when running test cases. Cypress clears any existing screenshots before Cypress runs.</p><p><strong>support</strong>: We can add custom commands and import them later when use them in tests.</p><p><strong>videos</strong>: Videos are stored in this folder by default when running test cases. Cypress clears any existing videos before Cypress runs.</p><p><strong>cypress.config.js</strong>: This file is created in the application root folder when configuring cypress into a project. This can be used to modify configurations dynamically which is defined from Cypress configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*J1TwzVz8559bOgmc0jNwOw.png" /></figure><p><strong>Test Structure</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*Y9jt07u977sLEK-MNm8vSA.png" /></figure><p>In React, test files are saved with the “<strong>.<em>cy.js</em></strong>“ extension.</p><p>Ex: — demoTest.cy.js</p><p>To further cypress testing, let’s consider the following example.</p><p>1. Click on a button on the relevant web page.</p><p>2. Load some information from the database and display it in the page view.</p><p>Need to test whether data is loaded on button click</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*xnoHpR1YCmCpZv1LSiWsKg.png" /><figcaption>Figure 05: Cypress Test Suit: demo.cy.js</figcaption></figure><p>Below is the cypress test suit structure written for the scenario.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*TKWCEgnbz9gQGcwLXMHJPg.png" /><figcaption>Figure 06: Test Suit Structure</figcaption></figure><p><strong>describe() </strong>and<strong> it()</strong></p><p>The test interface is defined with <strong>describe() </strong>and test cases are written within <strong>it()</strong> blocks. Each suit can have multiple test cases. There is no limit exceeded.</p><p><strong>beforeEach()</strong></p><p>It is a hook used in Cypress, which helps to set conditions and API calls that want to run before the set of tests.</p><p>The most used Cypress hooks are:</p><p><strong>1.</strong> <strong><em>before()</em></strong><em> </em>: Run once before all tests in the block</p><p><strong>2.</strong> <strong><em>beforeEach()</em></strong> : Run before each test in the block</p><p><strong>3.</strong><em> </em><strong><em>after()</em></strong><em> </em>: Run once after all tests in the block</p><p><strong>afterEach()</strong> : Run after each test in the block</p><p><strong>intercept</strong></p><p>By intercepting network requests, testers <strong><em>can test how the application handles API calls</em> </strong>(HTTP requests and responses). Testing error response handling, response time, and response code are included in intercepting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*CZCLhaVK-Bzl6m4Dnu83_w.png" /></figure><p><strong>method:</strong> HTTP request method. This can be any of POST, GET, PUT, DELETE</p><p><strong>URL</strong>: HTTP request URL</p><p><strong><em>response</em></strong>: This represents the mocked response of the request. Inside the response block, there are two variables: <strong>statusCode and fixture.</strong></p><p><strong>1.</strong> <strong>statusCode</strong>: Status of the response (200, 201, 500 etc.)</p><p>Ex: — If test success scenarios: 200 (GET), 201 (POST)</p><p>If test error scenarios: 500,404</p><p><strong>2.</strong> <strong>fixture</strong>: Related mock <strong><em>.json</em></strong> file path for the response should be mentioned here. Mock files are created inside the fixture folder.</p><p><strong>Cypress assertions and actions</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*EA0l-8Fc-pyhsXDEffxAcA.png" /></figure><p><strong>Assertions</strong></p><p>It is not enough to create a test case and provide commands to each testing workflow. There should be assertions to ensure each component renders correctly, behaves correctly, or to validate API responses are retrieved correctly, etc. Assertions are the things that we hope to test in each test case and results are generate on the pass rate of assertions.</p><p>Below are some of the most used assertions in Cypress.</p><ol><li><strong><em>.should(‘exist’)</em></strong> : Verify that the element exists on the page</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*BDvy_XTUtLfZQc1HnSgYtQ.png" /></figure><p><strong><em>2. .should(‘have.class’, classname)</em></strong> : Verify that an element has a specific class</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*4amcjxc8CAZpNByrvywA7w.png" /></figure><p>3. <strong><em>.should(‘be’, ’visible’)</em></strong><em> : Verify the element is visible</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*Nyh48JjY4uQEFlUHqbBDYw.png" /></figure><p>4. <strong><em>.should(‘contain’, ’title’)</em></strong><em> : Verify the specific element content is available</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*n45HYHazF9IJO-QaZfD1Xg.png" /></figure><p>5. <strong><em>.should(‘be’, ’disabled’)</em></strong><em> : Verify the specific element is disabled</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*crm6K20wDTolUgTDaPtCRg.png" /></figure><p>6. <strong><em>.should(‘be’, ’enabled’)</em></strong><em> : Verify the specific element is enabled</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*LcJOOTSuhN3e0-H_xOGCpg.png" /></figure><p>7. <strong><em>.should(‘be’, ’checked’)</em></strong><em> : Verify the checkbox or radio button is checked</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*QK_4yMGmsQcBrYat_09DEQ.png" /></figure><p><strong>Actions</strong></p><p>Action commands in Cypress are for interacting with the DOM. These actions simulate a user interacting with the application.</p><p>Following are some of actions used in Cypress.</p><ol><li><strong><em>.click() </em></strong>:<strong><em> </em></strong>Simulate click action like clicking an element, button, etc.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*MjHAgd69__ocX1YxM8o4FQ.png" /></figure><p>2. <strong><em>.type()</em></strong> : Simulate getting input text to be typed</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*HQojoZcdBBkSO6TLYUBtpA.png" /></figure><p>3. <strong><em>.clear()</em></strong> : Clear an input field</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*iJQhDDCKqGM62uIdI17Vow.png" /></figure><p>4. <strong><em>.select()</em></strong> : Select a value from the dropdown list</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*7bM0gkcSqK1eNykR_E3nJA.png" /></figure><p>5. <strong><em>.selectFile()</em></strong> : Simulate browse feature. Can simulate single browsing or multiple browsing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*nzQmIhSaO8_a0gwhCk05Nw.png" /></figure><p><strong>Run Cypress Tests</strong></p><p>After completing the test case writing you should run Cypress in the terminal. There are two major types of run Cypress Testing: <strong><em>Testing in the browser</em></strong> and <strong><em>testing in headless mode</em></strong>.</p><p>Let’s look at both styles.</p><ol><li><strong>Testing in Browser</strong></li></ol><p>When starts running, the Cypress window is open in the browser, and it shows the <strong><em>component spec</em></strong> that we have written test cases.</p><p>Testing will start when only click on the Cypress test file that you want to execute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*XOZWLVBp4TwiIhcpffzmWA.png" /><figcaption>Figure 07: Click on the demo.cy.js file to execute</figcaption></figure><p>Testing results will be like the below after test cases are executed in the browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*OjJNY8PHJDOhqJ2AdARPaQ.png" /><figcaption><em>Figure 08:</em> Test Results when testing in the browser</figcaption></figure><p><strong><em>2. </em>Testing in Headless Mode</strong></p><p>To test with headless mode, run this headless command in the terminal.</p><p>The special of headless mode is that, with executing one command all cypress test suits within the project are executed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*cx_T0Lr85VJcDk0W-57CvQ.png" /></figure><p>Then individual test result summaries of each component will display like this including a summary of passed, failed, and skipped test cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*JAmu9zXs3-Qh3mXhe3JioQ.png" /><figcaption>Figure 09: Individual component test result in headless mode</figcaption></figure><p>The final test result summary report will provide a table view after completely executing all the Cypress tests within the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*q44BDAqXp0QGDXXjOu62Sg.png" /><figcaption>Figure 10: Test result report of the whole application in headless mode</figcaption></figure><p>Now, testing with Cypress is completed. You can customize your code related to the application functionalities.</p><p><strong>Make it easy with Cypress automation!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2557783f93ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>