<?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: RonFrontWeb</title>
    <description>The latest articles on DEV Community by RonFrontWeb (@ronfrontweb).</description>
    <link>https://dev.to/ronfrontweb</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%2F581591%2F2dac13f9-93c2-4eb5-8291-be37b5735e39.png</url>
      <title>DEV Community: RonFrontWeb</title>
      <link>https://dev.to/ronfrontweb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ronfrontweb"/>
    <language>en</language>
    <item>
      <title>A guide to testing and formatting in react
</title>
      <dc:creator>RonFrontWeb</dc:creator>
      <pubDate>Fri, 19 Feb 2021 11:00:00 +0000</pubDate>
      <link>https://dev.to/ronfrontweb/a-guide-to-testing-and-formatting-in-react-28ia</link>
      <guid>https://dev.to/ronfrontweb/a-guide-to-testing-and-formatting-in-react-28ia</guid>
      <description>&lt;h1&gt;
  
  
  A guide to testing and formatting in react
&lt;/h1&gt;

&lt;p&gt;Here is a tutorial that explain how to implement&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tests in react&lt;/li&gt;
&lt;li&gt;Pre-commit hooks for tests&lt;/li&gt;
&lt;li&gt;Prettier og rules for formatting in react&lt;/li&gt;
&lt;li&gt;Pre-commit hooks for prettier&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we begin you need to find a code editor&lt;br&gt;
For this tutorial i am using “visual studio code” but you decide which one you want to use&lt;/p&gt;

&lt;p&gt;Start by creating a folder , but remember that the name has to be in lowercase and The folder has to be completely empty or react will not accept it.&lt;/p&gt;

&lt;p&gt;Then you can open the terminal and enter the command ( npx create-react-app .)&lt;/p&gt;

&lt;p&gt;Now you can run (npm start) to test that react is working&lt;br&gt;
You should be at browser window popoing up with a page and the react logo.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing packages
&lt;/h2&gt;

&lt;p&gt;Now we need to install the rest of the npm packages with need for testing and formatting&lt;/p&gt;

&lt;p&gt;we are going to be using&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prettier (formatting)&lt;/li&gt;
&lt;li&gt;eslint-config-prettier (prettier add on)&lt;/li&gt;
&lt;li&gt;husky (controls precommits)&lt;/li&gt;
&lt;li&gt;cross environment (connects scripts across platforms)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Format packages
&lt;/h2&gt;

&lt;p&gt;first we will use the command &lt;code&gt;npm i -D prettier&lt;/code&gt; to install prettier and then the add on &lt;code&gt;npm i -D eslint-config-prettier&lt;/code&gt;&lt;br&gt;
that turns off all rules that are unnecessary or might conflict with Prettier&lt;/p&gt;

&lt;p&gt;and that is our formatting done.&lt;/p&gt;
&lt;h2&gt;
  
  
  Pre-commit packages
&lt;/h2&gt;

&lt;p&gt;now for the pre-commit packages&lt;/p&gt;

&lt;p&gt;we are going to install &lt;code&gt;npx mrm lint-staged&lt;/code&gt; and this will install husky and lint-staged packages and update your package.json&lt;/p&gt;

&lt;p&gt;after the first one we are going to install using this command &lt;code&gt;npx husky install&lt;/code&gt;&lt;br&gt;
this will help us make sure that our code is without errors and prevent that code from being uploaded and give you information to fix the issue so that you can commit working code.&lt;/p&gt;

&lt;p&gt;for the last install we are going to write &lt;code&gt;npx husky add .husky/pre-commit "npm test"&lt;/code&gt; and this will create a file in the folder .husky called pre-commit and adds the command "npm test"&lt;/p&gt;
&lt;h2&gt;
  
  
  Installer cross-env
&lt;/h2&gt;

&lt;p&gt;we need to install &lt;code&gt;npm i -D cross-env&lt;/code&gt; and short what this does is end our test when is has finished and allows us to commit&lt;/p&gt;

&lt;p&gt;after we manually add to our package.json these two commmads to scripts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"test": "cross-env CI=true react-scripts test"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"prettier": "prettier --write ."&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and to the pre-commit file&lt;/p&gt;

&lt;p&gt;add - &lt;code&gt;npm run prettier&lt;/code&gt; and this runs prettier if the test is succesfull&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up a test on a component
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Here we see a simple component
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Navbar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;nav&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;About&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Store&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/nav&lt;/span&gt;&lt;span class="err"&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;Navbar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;now let's see how a test looks&lt;br&gt;
here we are testing to see if the component &lt;code&gt;Navbar.js&lt;/code&gt; is rendering "home" in the browser&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;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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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="nx"&gt;Navbar&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Navbar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;check to see if text home is rendered&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;render&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;Navbar&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;checker&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="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/Home/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;checker&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&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;so when we run &lt;code&gt;npm test&lt;/code&gt; we will get&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PASS src/Navbar.test.js
  ✓ check to see if text home is rendered (33 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.878 s, estimated 2 s
Ran all test suites.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here we see the test is passed&lt;/p&gt;

&lt;p&gt;if the code were to fail it would look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FAIL src/Navbar.test.js
  ✕ check to see if text home is rendered (34 ms)

  ● check to see if text home is rendered

    TestingLibraryElementError: Unable to find an element with the text: /Homie/i. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

    &amp;lt;body&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;nav&amp;gt;
          &amp;lt;ul&amp;gt;
            &amp;lt;li&amp;gt;
              &amp;lt;a
                href="#"
              &amp;gt;
                Home
              &amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;
              &amp;lt;a
                href="#"
              &amp;gt;
                About
              &amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;
              &amp;lt;a
                href="#"
              &amp;gt;
                Store
              &amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;
              &amp;lt;a
                href="#"
              &amp;gt;
                Contact
              &amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
          &amp;lt;/ul&amp;gt;
        &amp;lt;/nav&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/body&amp;gt;

       5 | test('check to see if text home is rendered', () =&amp;gt; {
       6 |   render(&amp;lt;Navbar /&amp;gt;);
    &amp;gt;  7 |   const checker = screen.getByText(/Homie/i);
         |                          ^
       8 |   expect(checker).toBeInTheDocument();
       9 | });
      10 |

      at Object.getElementError (node_modules/@testing-library/dom/dist/config.js:37:19)
      at node_modules/@testing-library/dom/dist/query-helpers.js:90:38
      at node_modules/@testing-library/dom/dist/query-helpers.js:62:17
      at getByText (node_modules/@testing-library/dom/dist/query-helpers.js:111:19)
      at Object.&amp;lt;anonymous&amp;gt; (src/Navbar.test.js:7:26)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.952 s, estimated 2 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It tells us what kind of error we are dealing with and where to locate it&lt;br&gt;
so we can fix it.&lt;/p&gt;

&lt;p&gt;in this example we changed home to homie so the test will fail because it can't find &lt;code&gt;home&lt;/code&gt;&lt;br&gt;
and when we correct the error our test will pass.&lt;/p&gt;

&lt;p&gt;now lets have a look at prettier and what that does for our code&lt;/p&gt;

&lt;p&gt;this example of a code that is hard to read because of poor formatting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Navbar(props) {
    return (
    &amp;lt;nav&amp;gt;
        &amp;lt;ul&amp;gt;
                &amp;lt;li&amp;gt;
        &amp;lt;a href="#"&amp;gt;Home&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
    &amp;lt;a href="#"&amp;gt;About&amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
                    &amp;lt;a href="#"&amp;gt;Store&amp;lt;/a&amp;gt;
        &amp;lt;/li&amp;gt;
        &amp;lt;li&amp;gt;
                    &amp;lt;a href="#"&amp;gt;Contact&amp;lt;/a&amp;gt;
        &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
        &amp;lt;/nav&amp;gt;
    );
}

export default Navbar;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but because we use prettier wich run with our &lt;code&gt;git commit -m "message"&lt;/code&gt;&lt;br&gt;
so it will fix our poor formatting for us and make it look like this and we get nice readable code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Navbar(props) {
    return (
        &amp;lt;nav&amp;gt;
            &amp;lt;ul&amp;gt;
                &amp;lt;li&amp;gt;
                    &amp;lt;a href="#"&amp;gt;Home&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
                    &amp;lt;a href="#"&amp;gt;About&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
                    &amp;lt;a href="#"&amp;gt;Store&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
                &amp;lt;li&amp;gt;
                    &amp;lt;a href="#"&amp;gt;Contact&amp;lt;/a&amp;gt;
                &amp;lt;/li&amp;gt;
            &amp;lt;/ul&amp;gt;
        &amp;lt;/nav&amp;gt;
    );
}

export default Navbar;

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

&lt;/div&gt;



&lt;p&gt;but if the test doesn't pass you can't commit and prettier will not run.&lt;/p&gt;

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