<?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>DEV Community: Stamatis Deliyannis</title>
    <description>The latest articles on DEV Community by Stamatis Deliyannis (@stamatisdeli).</description>
    <link>https://dev.to/stamatisdeli</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%2F841987%2F81feb15d-99bf-4109-bdee-cec2f894fe10.png</url>
      <title>DEV Community: Stamatis Deliyannis</title>
      <link>https://dev.to/stamatisdeli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stamatisdeli"/>
    <language>en</language>
    <item>
      <title>Component Testing with Cypress and Reactjs</title>
      <dc:creator>Stamatis Deliyannis</dc:creator>
      <pubDate>Sun, 03 Sep 2023 11:26:38 +0000</pubDate>
      <link>https://dev.to/stamatisdeli/react-component-testing-with-cypress-5dm8</link>
      <guid>https://dev.to/stamatisdeli/react-component-testing-with-cypress-5dm8</guid>
      <description>&lt;p&gt;Cypress has a feature called &lt;a href="https://docs.cypress.io/guides/component-testing/overview"&gt;Component Testing&lt;/a&gt;, and I found the experience using it quite interesting, and familiar for those coming from non-UI testing suites. I assume that you have basic knowledge of Reactjs and Cypress and perhaps &lt;a href="https://testing-library.com/docs/react-testing-library/intro/"&gt;React Testing Library&lt;/a&gt; and &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; so I will skip the details and focus on the core concept.&lt;/p&gt;

&lt;p&gt;Let us assume we have created a &lt;code&gt;Parent&lt;/code&gt; component that renders a &lt;code&gt;Child&lt;/code&gt; component. &lt;code&gt;Parent&lt;/code&gt; makes an API request to a backend, stores it in state, and renders it to &lt;code&gt;Child&lt;/code&gt; via props. We are adding some tests to make sure that the &lt;code&gt;Child&lt;/code&gt; properly renders the data, and nothing crashes if some part of the data is missing. Simple stuff, typical example.&lt;/p&gt;

&lt;p&gt;For our testing purposes we don't need a proper backend API to make our request since we're going to stub it. We can write something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/users/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and then we call &lt;code&gt;getData&lt;/code&gt; in our useEffect, store the user object in state, pass the user as a prop to our child component, blah blah, you know the part. Now let's get down to business!&lt;/p&gt;

&lt;p&gt;We start writing our mock data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MOCK_DATA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;33265 Kirlin Center&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Enoch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Graham&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Port Neha&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Berkshire&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since Parent is making the request, we need to use the &lt;a href="https://docs.cypress.io/api/commands/intercept#Intercepted-requests"&gt;intercept&lt;/a&gt; command to stub the response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;Parent/&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renders&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/users/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MOCK_DATA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;---&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are now ready to add more assertions by modifying the &lt;code&gt;MOCK_DATA&lt;/code&gt; object, for example to test what happens when &lt;code&gt;address&lt;/code&gt; is undefined.&lt;/p&gt;

&lt;p&gt;With the React Testing Library and Jest, we would do pretty much the same thing: We would use something like &lt;a href="https://jestjs.io/docs/jest-object#jestspyonobject-methodname"&gt;jest.spyOn&lt;/a&gt; in combination with &lt;a href="https://jestjs.io/docs/mock-function-api/#mockfnmockresolvedvaluevalue"&gt;mockResolvedValue&lt;/a&gt; to intercept the request and mock the response. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is great about Cypress Component Testing is that we have a visual representation of our test with simple methods, which is a big enhacement in terms of the development experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Some notes on configuring Cypress, and improving your testing strategy:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When Cypress creates a component test, you will notice that the styles are not applied. Simply follow the relevant &lt;a href="https://docs.cypress.io/guides/component-testing/styling-components"&gt;documentation&lt;/a&gt; to solve this.&lt;/li&gt;
&lt;li&gt;If you are using Typescript, you will probably need to configure &lt;code&gt;tsconfig.json&lt;/code&gt; so that Cypress types are recognized.&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://fakerjs.dev/"&gt;faker.js&lt;/a&gt; and factories to generate random values for your mock data. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>react</category>
      <category>testing</category>
      <category>component</category>
    </item>
  </channel>
</rss>
