<?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: Rizul Sharma</title>
    <description>The latest articles on DEV Community by Rizul Sharma (@rizul_sharma).</description>
    <link>https://dev.to/rizul_sharma</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%2F2796487%2Fc0d99367-15f1-43ac-9ee5-5082acdf0bdc.gif</url>
      <title>DEV Community: Rizul Sharma</title>
      <link>https://dev.to/rizul_sharma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rizul_sharma"/>
    <language>en</language>
    <item>
      <title>Understanding Nested Describe Blocks in Unit Tests</title>
      <dc:creator>Rizul Sharma</dc:creator>
      <pubDate>Fri, 16 May 2025 12:38:58 +0000</pubDate>
      <link>https://dev.to/rizul_sharma/understanding-nested-describe-blocks-in-unit-tests-4n4l</link>
      <guid>https://dev.to/rizul_sharma/understanding-nested-describe-blocks-in-unit-tests-4n4l</guid>
      <description>&lt;p&gt;When writing unit tests for JavaScript applications, organizing your tests properly can greatly improve maintainability and readability. One common technique uses nested &lt;code&gt;describe&lt;/code&gt; blocks to group related test cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Impact on Coverage Reports
&lt;/h2&gt;

&lt;p&gt;Nested describe blocks &lt;strong&gt;do not cause any issues&lt;/strong&gt; with code coverage reports or coverage UIs in modern testing frameworks like Jest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Coverage tools like Jest/Istanbul measure which lines of code are executed during tests, regardless of how those tests are organized&lt;/li&gt;
&lt;li&gt;The nested structure doesn't affect which code is executed or how it's measured&lt;/li&gt;
&lt;li&gt;Test organization is purely for human readability and doesn't impact coverage metrics&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Benefits of Nested Describe Blocks
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Better Organization&lt;/strong&gt;: Groups related tests logically by function or behavior&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scoped Setup/Teardown&lt;/strong&gt;: You can use &lt;code&gt;beforeEach&lt;/code&gt;/&lt;code&gt;afterEach&lt;/code&gt; at different levels of nesting&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Test Reports&lt;/strong&gt;: Makes test output more readable with hierarchical grouping&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier Filtering&lt;/strong&gt;: Allows running specific groups of tests with most test runners&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear Documentation&lt;/strong&gt;: Test output effectively documents the code's expected behavior&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  When to Use Nested Describes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flh5gyi85kp8y7ygotj9p.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flh5gyi85kp8y7ygotj9p.jpeg" alt="Coder thinking" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Nested Describe Blocks When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Testing Multiple Functions&lt;/strong&gt;: When a file contains multiple functions, use a nested structure to organize tests by function&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complex Behavior&lt;/strong&gt;: When testing different aspects or scenarios of a complex function&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared Setup&lt;/strong&gt;: When different test groups need different setup/teardown code&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use Single Describe Block When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single Function&lt;/strong&gt;: When testing a single, simple function with straightforward behavior&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small Component&lt;/strong&gt;: For small UI components with limited functionality&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flat Structure Preference&lt;/strong&gt;: When team standards prefer flatter structures for consistency&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep nesting to 2-3 levels maximum for readability&lt;/li&gt;
&lt;li&gt;Use consistent naming patterns (e.g., "FunctionName", "when condition", "should behavior")&lt;/li&gt;
&lt;li&gt;Name the describe blocks clearly to make the test hierarchy meaningful&lt;/li&gt;
&lt;li&gt;Consider the displayed output in your CI/CD system and test reports&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Examples
&lt;/h2&gt;

&lt;p&gt;See the accompanying example files for demonstrations of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Multiple functions with nested blocks&lt;/strong&gt;: &lt;code&gt;calculator.test.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single function with flat structure&lt;/strong&gt;: &lt;code&gt;formatter.test.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;calculator.test.js&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmo8royr36vo9nfqir2xh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmo8royr36vo9nfqir2xh.png" alt="calculator.test.js" width="800" height="831"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;formatter.test.js&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkilonuxsjauwzzt8rox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhkilonuxsjauwzzt8rox.png" alt="formatter.test.js" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Nested Describe Blocks (Multiple Functions)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt; For files with multiple functions or complex behavior&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Improved Organization&lt;/strong&gt;: Groups tests logically by function name, making it easy to locate tests for specific functionality&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hierarchical Reports&lt;/strong&gt;: Creates a well-structured test output that mirrors your code organization, making failures easier to locate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolated Test Context&lt;/strong&gt;: Allows function-specific setup/teardown code and avoids test pollution between different function tests&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better Documentation&lt;/strong&gt;: The nested structure serves as living documentation for how different parts of your module should behave&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Flat Structure (Single Function)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt; For files with a single function or simple components&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;: Avoids unnecessary nesting when all tests relate to the same function, making the test file more readable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flatter Output&lt;/strong&gt;: Produces cleaner, more concise test reports without redundant grouping&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier Scanning&lt;/strong&gt;: All test cases appear at the same level, making it easier to quickly scan all the behaviors being tested&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduced Verbosity&lt;/strong&gt;: Prevents the extra indentation and boilerplate that comes with multiple nested blocks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both approaches have their place in a well-organized test suite - the key is choosing the right structure for each specific situation.&lt;/p&gt;

&lt;p&gt;By choosing the right structure for your tests, you can significantly improve your test suite's readability, maintainability, and usefulness as documentation.&lt;/p&gt;



















&lt;h2&gt;
  
  
  Extra Read (Coverage Report Summary: Nested vs Flat Describe Blocks):
&lt;/h2&gt;

&lt;p&gt;Coverage reports are generally &lt;strong&gt;identical&lt;/strong&gt; regardless of how you structure your describe blocks, because coverage tools care about which lines execute, not how tests are organized. However, there are some subtle differences in the reports:&lt;/p&gt;

&lt;h2&gt;
  
  
  Coverage with Nested Describe Blocks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No Impact on Metrics&lt;/strong&gt;: Coverage percentages remain exactly the same as with flat structure&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test Names in Reports&lt;/strong&gt;: Coverage tools may show longer, concatenated test names in failure reports (e.g., "Calculator add should handle negative numbers")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filtering Coverage Reports&lt;/strong&gt;: Some tools allow filtering coverage by describe blocks, making it easier to focus on specific function coverage&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coverage Detail View&lt;/strong&gt;: When examining uncovered lines, the hierarchical test names may provide more context about what functionality is missing coverage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Coverage with Flat Describe Blocks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Identical Coverage Metrics&lt;/strong&gt;: The same lines, branches, functions, and statements are covered&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simpler Test Names&lt;/strong&gt;: Coverage failures reference shorter test names, which can be easier to read in some reports&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Direct Mapping&lt;/strong&gt;: In simple files, the flat structure may map more clearly to coverage reports that don't support hierarchical views&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cleaner Reports&lt;/strong&gt;: Some older coverage tools may display flat structure test results more cleanly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key insight is that the &lt;strong&gt;actual coverage numbers are identical&lt;/strong&gt; regardless of structure - it's mainly about how readable you want your test code and reports to be for your specific situation.&lt;/p&gt;

&lt;p&gt;If your coverage tool allows hierarchical filtering of reports, nested describes can make it easier to understand coverage at different functional levels.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>🚀 Auto-Generate Swagger Documentation for Your Java Microservice</title>
      <dc:creator>Rizul Sharma</dc:creator>
      <pubDate>Fri, 09 May 2025 05:15:11 +0000</pubDate>
      <link>https://dev.to/rizul_sharma/auto-generate-swagger-documentation-for-your-java-microservice-2ke6</link>
      <guid>https://dev.to/rizul_sharma/auto-generate-swagger-documentation-for-your-java-microservice-2ke6</guid>
      <description>&lt;p&gt;Manually writing Swagger docs? 😓 It's time to level up your workflow and let your &lt;strong&gt;code generate the docs for you&lt;/strong&gt; — automatically!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vjgyelj0hgxlkk1y03t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vjgyelj0hgxlkk1y03t.png" alt="cover" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're using &lt;strong&gt;Spring Boot&lt;/strong&gt;, the best way to do this is with:&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ Springdoc OpenAPI
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🔧 What It Does
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Springdoc OpenAPI&lt;/strong&gt; is a free, open-source integration that &lt;strong&gt;automatically generates Swagger UI and OpenAPI 3 documentation&lt;/strong&gt; from your Spring Boot REST APIs. No manual effort required — just annotate your controllers as usual!&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠 How to Set It Up
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Add Dependency in &lt;code&gt;pom.xml&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springdoc&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;springdoc-openapi-ui&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.7.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt; &lt;span class="c"&gt;&amp;lt;!-- use latest --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;📦 Works with Maven — use the corresponding dependency for Gradle if needed.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  2. Run Your Application and Access Swagger UI
&lt;/h3&gt;

&lt;p&gt;Once your app is running, open your browser and visit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:8080/swagger-ui/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;🎉 You'll see a full, interactive Swagger UI with all your API endpoints, parameters, responses, and models!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ✅ What It Supports
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;REST Controllers (&lt;code&gt;@RestController&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Mappings like &lt;code&gt;@GetMapping&lt;/code&gt;, &lt;code&gt;@PostMapping&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;Bean validation annotations (&lt;code&gt;@NotNull&lt;/code&gt;, &lt;code&gt;@Size&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;Spring Security annotations (with extra config)&lt;/li&gt;
&lt;li&gt;Custom documentation with:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;@Operation&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@ApiResponse&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@Parameter&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔍 Bonus: Get Raw OpenAPI Spec
&lt;/h2&gt;

&lt;p&gt;For exporting your API or generating clients:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;http://localhost:8080/v3/api-docs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Returns the OpenAPI 3 JSON spec!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ✨ Example: Auto-Documented Controller
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getAllUsers&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAllUsers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nd"&gt;@Valid&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;userService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The above controller will automatically show in Swagger UI — no extra setup needed!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🙌 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Using &lt;strong&gt;Springdoc OpenAPI&lt;/strong&gt; is a quick win for any Java microservice project. It ensures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Always up-to-date documentation&lt;/li&gt;
&lt;li&gt;🤝 Easier collaboration with frontend/backend/devops&lt;/li&gt;
&lt;li&gt;🚀 Faster onboarding for new devs&lt;/li&gt;
&lt;li&gt;🧹 Eliminates outdated manual Swagger files&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 Useful Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/springdoc/springdoc-openapi" rel="noopener noreferrer"&gt;Springdoc OpenAPI GitHub Repo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://springdoc.org/" rel="noopener noreferrer"&gt;Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://petstore.swagger.io/" rel="noopener noreferrer"&gt;Swagger UI Demo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://swagger.io/specification/" rel="noopener noreferrer"&gt;OpenAPI 3.0 Spec&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
    </item>
    <item>
      <title>Level Up Your React Code: A Friendly Guide to Unit Testing Best Practices</title>
      <dc:creator>Rizul Sharma</dc:creator>
      <pubDate>Thu, 08 May 2025 10:06:56 +0000</pubDate>
      <link>https://dev.to/rizul_sharma/level-up-your-react-code-a-friendly-guide-to-unit-testing-best-practices-44lh</link>
      <guid>https://dev.to/rizul_sharma/level-up-your-react-code-a-friendly-guide-to-unit-testing-best-practices-44lh</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5y3anjp1ku6ylcnbvz05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5y3anjp1ku6ylcnbvz05.png" alt="React Testing" width="288" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey there, fellow React developers! 👋 Ready to make your components more robust, your refactoring less scary, and your development workflow smoother? Then pull up a chair, because we're diving into the wonderful world of React unit testing! This isn't just a chore; it's your &lt;strong&gt;secret weapon&lt;/strong&gt; for building high-quality, maintainable front-end applications.&lt;/p&gt;

&lt;p&gt;Let's break down what testing is all about and how you can master it in your React projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What Exactly Are Test Cases?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you've just built a brand new, shiny button component in your React app. It's supposed to change color when you click it. How do you know it &lt;em&gt;actually&lt;/em&gt; does that? You click it, right?&lt;/p&gt;

&lt;p&gt;A test case is essentially a formalized version of this manual check. In software development, a test case is a set of conditions or actions performed on a software component or system to verify that it functions correctly according to its requirements. Think of it as a &lt;strong&gt;mini-experiment&lt;/strong&gt; designed to confirm a specific piece of functionality behaves as expected.&lt;/p&gt;

&lt;p&gt;Each test case should have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A clear purpose:&lt;/strong&gt; What specific behavior are you trying to verify?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Setup steps (Arrange):&lt;/strong&gt; What needs to be in place before you run the test? (e.g., rendering a component with certain props).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Actions (Act):&lt;/strong&gt; What do you do to trigger the behavior you're testing? (e.g., clicking a button, typing in an input).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expected outcome (Assert):&lt;/strong&gt; What should happen after you perform the actions? (e.g., the button's class changes, a specific text appears).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why Bother with Testing, Especially for Front-End?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzri6kw8j549oj2uk2tpg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzri6kw8j549oj2uk2tpg.jpeg" alt="Why Bother" width="800" height="800"&gt;&lt;/a&gt;&lt;br&gt;
Okay, writing tests takes extra time upfront. So, why is it considered a &lt;strong&gt;non-negotiable best practice&lt;/strong&gt;, especially for the dynamic world of front-end development with libraries like React?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Catch Bugs Early (The Earlier, The Better!):&lt;/strong&gt; Finding bugs &lt;em&gt;before&lt;/em&gt; your users do is a massive win. Automated tests run quickly and consistently, flagging issues the moment you introduce them, saving you countless hours of debugging down the line. For complex UIs with intricate state management and interactions, this is &lt;strong&gt;invaluable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confidence to Refactor (Fearless Code Transformation):&lt;/strong&gt; Ever dread changing a piece of code because you're afraid of breaking something elsewhere? Tests act as a safety net. If your tests pass after refactoring, you can be confident you haven't introduced regressions (new bugs in existing features). This allows you to &lt;strong&gt;improve your codebase over time&lt;/strong&gt; without anxiety.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Code Quality and Design (Building Better Software):&lt;/strong&gt; Writing testable code often leads to better-designed code. When you think about how to test a component in isolation, you naturally lean towards making it more modular, with clear inputs and outputs. This fosters a &lt;strong&gt;cleaner architecture&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation in Action (Living Examples):&lt;/strong&gt; Well-written tests serve as living documentation for your components. By looking at the tests, other developers (or even your future self!) can quickly understand how a component is supposed to be used and what its expected behavior is. It's like having &lt;strong&gt;executable documentation&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smoother Collaboration (Working Together, Seamlessly):&lt;/strong&gt; In a team environment, tests provide a shared understanding of how the application should behave. When someone makes a change, running the test suite confirms they haven't broken existing functionality, making collaboration &lt;strong&gt;much smoother&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For front-end applications, where user interaction and visual feedback are key, testing ensures that your UI components not only render correctly but also respond as expected to user actions and data changes. It's about guaranteeing a &lt;strong&gt;delightful and reliable user experience&lt;/strong&gt;.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;Types of Tests and the Tools of the Trade&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3m0n764kepjj8y7g695.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3m0n764kepjj8y7g695.png" alt="Testing Pyramid" width="381" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Software testing exists on a spectrum, often visualized as a testing pyramid. The most common types you'll encounter in React development are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Unit Tests:&lt;/strong&gt; These are the smallest, fastest tests. They focus on testing individual, isolated units of code, like a single function or a single React component in isolation. The goal is to verify that each unit works correctly on its own.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Think:&lt;/strong&gt; Testing a function that formats a date, or testing a button component to ensure it renders with the correct text and handles a click event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Popular Tools for React Unit Testing:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Jest:&lt;/strong&gt; A powerful JavaScript test runner developed by Facebook (Meta). It's often the default choice for React projects and provides a testing framework, assertion library, and mocking capabilities out-of-the-box. Jest is known for its speed and ease of setup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React Testing Library:&lt;/strong&gt; This library focuses on testing React components from a user's perspective. Instead of dealing with component instances or internal state (like some older libraries), it encourages querying the DOM like a user would. This leads to more robust tests that are less likely to break when the component's internal implementation changes. &lt;strong&gt;Highly recommended for modern React testing.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Integration Tests:&lt;/strong&gt; These tests verify that different units or components work together correctly. They test the interactions between connected parts of your application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Think:&lt;/strong&gt; Testing that a form component correctly handles input changes and submits data to an API (even if the API call itself is mocked). Testing that a parent component correctly passes data to and receives events from a child component.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools:&lt;/strong&gt; Jest and React Testing Library are also commonly used for integration testing of React components.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;End-to-End (E2E) Tests:&lt;/strong&gt; These are the highest level of tests. They simulate a real user's journey through your entire application, from the initial page load to completing a specific task. They test the application as a whole, including the front-end, back-end, and database interactions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Think:&lt;/strong&gt; Testing a user signing up, logging in, adding an item to a cart, and completing a purchase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Popular Tools:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cypress:&lt;/strong&gt; A popular, developer-friendly E2E testing framework.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Playwright:&lt;/strong&gt; Developed by Microsoft, also a strong contender for E2E testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;Choosing Your Testing Strategy and Tools: It's Not One-Size-Fits-All!&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Selecting the right testing strategy and tools depends on several factors unique to your project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Project Size and Complexity:&lt;/strong&gt; A small marketing website might require fewer tests than a large, complex e-commerce platform or a financial application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Team Expertise and Familiarity:&lt;/strong&gt; Choose tools that your team is comfortable with or can easily learn. The goal is to write tests effectively, not struggle with tooling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Project Requirements and Criticality:&lt;/strong&gt; For applications dealing with sensitive data or critical user flows, a higher level of test coverage and potentially more E2E tests might be necessary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Development Workflow and Speed:&lt;/strong&gt; Consider how quickly the tests run and how well they integrate into your development cycle (e.g., continuous integration).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A common and highly effective strategy for React applications is to focus on the base of the testing pyramid:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Prioritize Unit and Integration Tests with Jest and React Testing Library:&lt;/strong&gt; This combination is often the &lt;strong&gt;sweet spot&lt;/strong&gt; for React. Jest provides the reliable test runner, and React Testing Library helps you write tests that are focused on user behavior, making them more meaningful and less fragile. Start by ensuring your individual components and their interactions work correctly. This gives you a &lt;strong&gt;strong foundation of confidence&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add End-to-End Tests for Critical User Flows:&lt;/strong&gt; E2E tests are slower and more complex to maintain, so focus them on the most important paths users take in your application. These act as a final check on the integrated system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consider Snapshot Testing for UI Consistency:&lt;/strong&gt; Use snapshot tests strategically for components where maintaining a consistent structure is important (e.g., design system components). Be mindful of "snapshot fatigue" if they become too numerous or fragile.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explore Visual Regression Testing for Design Accuracy:&lt;/strong&gt; If visual fidelity is critical, add visual regression tests to catch unintended style or layout changes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The key is to find the right balance&lt;/strong&gt; that provides sufficient confidence without becoming a burden on development speed. Start with unit and integration tests, build good habits, and then layer on other types of tests as needed.&lt;/p&gt;


&lt;h2&gt;
  
  
  Show Me the Code! React Unit Test Examples
&lt;/h2&gt;

&lt;p&gt;First, make sure you have the necessary packages installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; jest @testing-library/react @testing-library/jest-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s dive into a &lt;strong&gt;Button&lt;/strong&gt; component example, utilizing &lt;code&gt;data-testid&lt;/code&gt; for easier targeting:&lt;/p&gt;

&lt;h3&gt;
  
  
  Button Component (&lt;code&gt;Button.js&lt;/code&gt;)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;disabled&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;data-testid&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"custom-button"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Unit Tests for the Button Component (&lt;code&gt;Button.test.js&lt;/code&gt;)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fireEvent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@testing-library/jest-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&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;Button Component&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="nf"&gt;test&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 with the correct text&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click Me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;custom-button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttonElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;calls the onClick prop when clicked&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;custom-button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttonElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalledTimes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;is disabled when disabled prop is true&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;disabled&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Disabled&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buttonElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getByTestId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;custom-button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttonElement&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toBeDisabled&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;fireEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buttonElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toHaveBeenCalled&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;✅ &lt;strong&gt;Key Takeaway:&lt;/strong&gt; Using &lt;code&gt;data-testid&lt;/code&gt; helps select elements more reliably, especially when elements don’t have accessible roles or clear labels. However, use them sparingly—prefer user-facing queries like &lt;code&gt;getByRole&lt;/code&gt;, &lt;code&gt;getByText&lt;/code&gt;, or &lt;code&gt;getByLabelText&lt;/code&gt; when possible to keep tests more maintainable and accessible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Other Helpful Testing Resources (Recommended Reads and watch)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;📝 &lt;a href="https://testing-library.com/docs/react-testing-library/intro/" rel="noopener noreferrer"&gt;React Testing Library Docs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📘 &lt;a href="https://jestjs.io/docs/getting-started" rel="noopener noreferrer"&gt;Jest Official Docs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🚀 &lt;a href="https://kentcdodds.com/blog/write-tests" rel="noopener noreferrer"&gt;Kent C. Dodds Blog on Testing&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;a href="https://blog.logrocket.com/guide-unit-testing-react-native/" rel="noopener noreferrer"&gt;How to write Unit Tests for React Native&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎯 &lt;a href="https://www.youtube.com/watch?v=FgnxcUQ5vho" rel="noopener noreferrer"&gt; 🖥️ Testing Crash Course – Fireship.io&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Happy Testing! 🧪🚀&lt;/p&gt;

</description>
      <category>react</category>
      <category>unittest</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
