<?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 Shruti Sahay on Medium]]></title>
        <description><![CDATA[Stories by Shruti Sahay on Medium]]></description>
        <link>https://medium.com/@shruti.sahay22?source=rss-bf9941ef40d1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UzJOR6S4amvNphsH.jpg</url>
            <title>Stories by Shruti Sahay on Medium</title>
            <link>https://medium.com/@shruti.sahay22?source=rss-bf9941ef40d1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:40:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shruti.sahay22/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[Using React Testing Library, MSW for testing React applications]]></title>
            <link>https://engg.glance.com/using-react-testing-library-msw-for-testing-react-applications-6e7c9fe710c8?source=rss-bf9941ef40d1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e7c9fe710c8</guid>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react-applications]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Shruti Sahay]]></dc:creator>
            <pubDate>Sun, 13 Nov 2022 10:26:06 GMT</pubDate>
            <atom:updated>2022-11-15T05:24:01.991Z</atom:updated>
            <content:encoded><![CDATA[<p>Any react application consists of number of components which club together to create a page and application. Basis this we primarily have 3 kinds of tests:</p><ul><li>Unit tests — Testing the major units of a page independently- Divide the page into its constituent logical components and write tests for each of it.</li><li>Integration tests — Application-level testing, which integrates all components together and tests the application on the whole by establishing communication between components.</li><li>End to end tests — A helper robot that behaves like a user to click around the app and verify that it functions correctly. Sometimes called “functional testing” or e2e.</li></ul><p>The testing trophy below explains the proportion by which all kinds of tests should be written. According to this, there should be maximum number of integration tests to cover the application most efficiently and capture most number of bugs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jstrxQo2gSl_VBNbLNPfEw.png" /></figure><p>Image courtesy — <a href="https://kentcdodds.com/blog/the-testing-trophy-and-testing-classifications">https://kentcdodds.com/blog/the-testing-trophy-and-testing-classifications</a></p><p><strong>React Testing Library</strong> is a testing utility tool that’s built to test the actual DOM tree rendered by React on the browser. This is mainly used for writing integration tests. With react testing library, we can:​</p><ul><li>Query your elements within the text, label, displayValue, role, and testId​</li><li>Fire any event​</li><li>Wait for an element to appear with wait​</li></ul><p><a href="https://mswjs.io/"><strong>M</strong></a><strong>ock Service Worker (MSW) </strong>is an API mocking library that uses the Service Worker API to intercept the actual requests. It is recommended by React Testing Library to make use of this library to mock server calls. Alternative to this includes mocking window.fetch but MSW acts as a much convenient way for server interaction over mocking ‘window.fetch’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vn32FjqSD1LAIEpm9Ked4g.png" /></figure><p>Image courtesy — <a href="https://redd.gitbook.io/msw/">https://redd.gitbook.io/msw/</a></p><p><strong>Prerequisites</strong>:</p><p>Install the following packages in your application:</p><ul><li>React testing Library​</li><li>Jest</li><li>Mock service worker</li><li>ts-jest (if using typescript)</li></ul><p>A React application created with Create React App (or CRA) already includes both React Testing Library and Jest by default. ​</p><p><strong>Code structure:</strong></p><p>The test code structure consists of the following kinds of files.</p><ul><li>Mock data files — These contain mock data response as the server would return in response to a particular request</li><li>Handler files — These capture server requests coming from test files and return the corresponding mock data required for the particular request.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JNVTHWiL20RNliiHdxVbjg.png" /></figure><ul><li>Test files — These are the primary unit testing files where we render the component/app, perform certain actions and expect that components are rendered fine</li><li>Setup file (setupTests)- Common/base contents for the tests can be put here so they are commonly accessed in all tests.</li></ul><p><strong>Getting started to write tests:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lLMU-UGqSDsDy1CjRAQ_Tw.png" /></figure><p>As mentioned in the diagram, we have a well defined flow for writing the tests.</p><ol><li>Render the component to be tested</li><li>Provide it particular set of data — This data can be provided to it in form of props or it can be data coming from server by intercepting network request by MSW from the handler file and returning it the expected set of data.</li><li>Mock functions inside component — There may be instances where you would need to mock a particular function as you don’t want the test to go inside the actual implementations or the test environment doesnt support the behaviours of actual browser. Eg — Intersection Observer, Video load. In such cases we mock these using jest.</li><li>Perform action — Select a particular element using queries provided by React Testing Library and fire the required event. ​</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HkJHaFlbT-uH-E_1xZdJIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HkJHaFlbT-uH-E_1xZdJIw.png" /></figure><p>Image courtesy — <a href="https://testing-library.com/docs/react-testing-library/cheatsheet">https://testing-library.com/docs/react-testing-library/cheatsheet</a></p><p>5. Wait for results — As we deal with asynchronous data, we need to wait for an element to appear or disappear in response to an event, user action, timeout, or Promise.The <a href="https://testing-library.com/docs/dom-testing-library/api-async/#waitfor">waitFor</a> method is a powerful asynchronous utility to enable us to make an assertion after a non-deterministic amount of time. The way waitFor works is that polls until the callback we pass stops throwing an error.​</p><p>6. Assertion — After performing the action, expect that the component has rerendered with the expected change on UI. It helps to check if there is a text change or for checking if an object has been modified etc.​ Jest provides huge list of matchers for this purpose.</p><p><strong>Run and debug:</strong></p><p>We can run the tests with any test runner, in our case it was Jest. React testing Library also provides useful tools for debugging our tests</p><ul><li>Automatic Logging — Whenever a test fails, it automatically prints the state of your DOM under test. Since the DOM size can get really large, you can set the limit of DOM content to be printed via environment variable DEBUG_PRINT_LIMIT. The default value is 7000.​</li><li>Screen.debug — Screen also exposes a debug method in addition to the queries. It supports debugging the document, a single element, or an array of elements.​</li></ul><p>Hope this information helps to kickstart using React testing library on your application. Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e7c9fe710c8" width="1" height="1" alt=""><hr><p><a href="https://engg.glance.com/using-react-testing-library-msw-for-testing-react-applications-6e7c9fe710c8">Using React Testing Library, MSW for testing React applications</a> was originally published in <a href="https://engg.glance.com/">Glance</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>