<?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: Alex Wardi</title>
    <description>The latest articles on DEV Community by Alex Wardi (@aaw5017).</description>
    <link>https://dev.to/aaw5017</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%2F135319%2F31fc128a-abc8-4d37-80d5-28beb0b37472.png</url>
      <title>DEV Community: Alex Wardi</title>
      <link>https://dev.to/aaw5017</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aaw5017"/>
    <language>en</language>
    <item>
      <title>Using Display Name to Debug Prop Type Warnings</title>
      <dc:creator>Alex Wardi</dc:creator>
      <pubDate>Sat, 09 Feb 2019 16:37:59 +0000</pubDate>
      <link>https://dev.to/aaw5017/using-display-name-to-debug-prop-type-warnings-58mh</link>
      <guid>https://dev.to/aaw5017/using-display-name-to-debug-prop-type-warnings-58mh</guid>
      <description>&lt;p&gt;For my React testing environment, I use Mocha, Enzyme, and Sinon. When I run my npm &lt;code&gt;test&lt;/code&gt; script, Mocha runs and enters "watch" mode.&lt;/p&gt;

&lt;p&gt;Recently, I noticed a strange and slightly annoying issue, where my &lt;em&gt;initial&lt;/em&gt; test run would display prop type warnings, but successive runs via watched file changes &lt;em&gt;would not&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I am using the &lt;code&gt;min&lt;/code&gt; reporter inside my mocha.opts file. Also, the component name shown below is respective to my code base, but we can assume any generic name, i.e. &lt;code&gt;MyComponent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's an example of the test output:&lt;/p&gt;

&lt;h5&gt;
  
  
  Initial Test Run
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxngt4yo9iir05pkljaym.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxngt4yo9iir05pkljaym.PNG" alt="Initial test run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Subsequent Test Runs (via watched files)
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Foy460u5d0wi0g84ztgnh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Foy460u5d0wi0g84ztgnh.PNG" alt="Subsequent test run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the second screenshot from above (a test run via a watched file change), I had not fixed the problematic prop types code within the offending component, yet my prop type warning disappeared. Why does this happen? It turns out, this is by design within the React code base. They only warn about props when a "new" component is rendered. Subsequent renders &lt;em&gt;of the same component&lt;/em&gt; will &lt;em&gt;NOT&lt;/em&gt; display warnings, as to avoid spamming the console or STDOUT.&lt;/p&gt;

&lt;p&gt;So how do we fix this? The answer is fairly simple, actually. We can take advantage of the &lt;code&gt;displayName&lt;/code&gt; property on the component, and update this within the &lt;code&gt;beforeEach&lt;/code&gt; hook for any given suite.&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="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;My suite&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;beforeEach&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;MyComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;displayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`MyComponent&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// Test code down here...&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when our tests run, React will interpret the component as "new", since its &lt;code&gt;displayName&lt;/code&gt; is changing. You'll see warnings on &lt;em&gt;every&lt;/em&gt; test run, rather than just the initial one. Here's an example of the output (cropped from right):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fuxm60caurrv0pl3ik6l8.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fuxm60caurrv0pl3ik6l8.PNG" alt="Test results after update"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you've fixed the warnings, just be sure to remove the &lt;code&gt;displayName&lt;/code&gt; code from your tests!&lt;/p&gt;

&lt;p&gt;Hopefully this helps you to quickly and somewhat painlessly fix any random PropTypes warnings in your Mocha tests!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/facebook/react/issues/7047" title="Github Issue Link" rel="noopener noreferrer"&gt;Github Issue&lt;/a&gt;&lt;/p&gt;

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