<?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: Santosh G</title>
    <description>The latest articles on DEV Community by Santosh G (@sant0shg).</description>
    <link>https://dev.to/sant0shg</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%2F191217%2Fc131ad84-46bf-4114-8db9-ebade4c9ce98.jpeg</url>
      <title>DEV Community: Santosh G</title>
      <link>https://dev.to/sant0shg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sant0shg"/>
    <language>en</language>
    <item>
      <title>Understanding brittle and non-brittle tests in react</title>
      <dc:creator>Santosh G</dc:creator>
      <pubDate>Sat, 06 Jul 2019 13:37:53 +0000</pubDate>
      <link>https://dev.to/sant0shg/understanding-brittle-and-non-brittle-tests-in-react-hjd</link>
      <guid>https://dev.to/sant0shg/understanding-brittle-and-non-brittle-tests-in-react-hjd</guid>
      <description>&lt;p&gt;Brittle tests test the implementation of the logic, while the non-brittle tests test the functionality. In the context of React, brittle tests usually test the change of state, or spy on some function to check if it is called. &lt;/p&gt;

&lt;p&gt;Let us see the code to understand the brittle and non-brittle tests in React application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends Component{
    state = {
        no: 0
    }

    incrementNo = () =&amp;gt; {
        const { no } = this.state;
        this.setState({
            no: no+1
        })
    }

    render(){
        const { no } = this.state;
        return(
            &amp;lt;div&amp;gt;
                &amp;lt;div data-testid="no"&amp;gt;
                    {no}
                &amp;lt;/div&amp;gt;
                &amp;lt;button data-testid="increment-button" onClick={this.incrementNo}&amp;gt;
                    Increment Number
                &amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        )
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Brittle test for the above React component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('should increment no - implementation test', () =&amp;gt; {
    const wrapper = shallow(&amp;lt;App /&amp;gt;);
    wrapper.instance().incrementNo();
    expect(wrapper.state('no')).toEqual(1);
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Non-brittle test for the above React component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it('should increment number - functionality test', () =&amp;gt; {
    const wrapper = shallow(&amp;lt;Brittle /&amp;gt;);
    expect(wrapper.find('[data-testid="increment-no"]').text()).toEqual('0')
    wrapper.find('[data-testid="increment-button"]').simulate('click');
    expect(wrapper.find('[data-testid="increment-no"]').text()).toEqual('1')
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Libraries supporting non-brittle tests
&lt;/h2&gt;

&lt;p&gt;There are many libraries that allow you to write tests in React. Some of the popular ones are &lt;a href="https://airbnb.io/enzyme/"&gt;Enzyme&lt;/a&gt; and &lt;a href="https://github.com/testing-library/react-testing-library"&gt;React Testing Library&lt;/a&gt;. &lt;br&gt;
The &lt;a href="https://airbnb.io/enzyme/"&gt;Enzyme&lt;/a&gt; library provides many APIs, which allow you to write tests the way you want. But &lt;a href="https://github.com/testing-library/react-testing-library"&gt;React Testing Library&lt;/a&gt; forces you to write non-brittle tests. Want to know more? I wrote another blog about differences between &lt;a href="https://techdoma.in/difference-between-react-test-utilities-and/"&gt;Enzyme and React testing library&lt;/a&gt;&lt;/p&gt;

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