<?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: MadsHaerup</title>
    <description>The latest articles on DEV Community by MadsHaerup (@madshaerup).</description>
    <link>https://dev.to/madshaerup</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%2F583463%2F2705b72f-606c-423d-af85-057d2bf855d7.png</url>
      <title>DEV Community: MadsHaerup</title>
      <link>https://dev.to/madshaerup</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/madshaerup"/>
    <language>en</language>
    <item>
      <title>Test-Driven Development with react and Jest</title>
      <dc:creator>MadsHaerup</dc:creator>
      <pubDate>Sun, 21 Feb 2021 16:19:22 +0000</pubDate>
      <link>https://dev.to/madshaerup/test-driven-development-with-react-1m5o</link>
      <guid>https://dev.to/madshaerup/test-driven-development-with-react-1m5o</guid>
      <description>&lt;p&gt;&lt;strong&gt;This will be a quick guide for automating the process of testing and formatting code, before committing.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The stack we will be using are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Prettier (which auto-formats code so you can stay focused on substance over style).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ESLint (which detects common coding mistakes). &lt;br&gt;
A linter is a program that uses a set of rules to detect code that, though&lt;br&gt;
syntactically valid, is likely to contain mistakes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Husky, is a tool for managing Git hooks in JavaScript projects. Git hooks are scripts that Git runs before or after certain commands, such as commit or push.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The core tenet of the software development methodology known as test-driven development (TDD) is to write the tests first.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TDD changes the coding experience by giving you rapid feedback:&lt;br&gt;
with your tests already in place, you can quickly find out what works and what doesn’t. That gives you the freedom to experiment with different approaches.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  &lt;strong&gt;Tests in react&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;If you start your react project with Create React App. It is ready to use and ships with Jest!&lt;/p&gt;

&lt;p&gt;With Jest as your test framework, you’ll be able to create a lightning-fast feedback loop.&lt;/p&gt;




&lt;h3&gt;
  
  
  Creating tests:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First step is to create a file and a test file:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--08dliGLW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uooe35yw3jsottusex4u.png" alt="image"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What a basic test consists of:&lt;/p&gt;

&lt;p&gt;❶ describe() declares a test suite, which is a grouping of tests. Its first argument&lt;br&gt;
is a name, and the second is a function containing one or more tests.&lt;/p&gt;

&lt;p&gt;❷ it() declares a test. Its first argument is a name, and the second is a function with the actual test code.&lt;/p&gt;

&lt;p&gt;❸ expect() creates an assertion. It takes a single argument, typically a value&lt;br&gt;
generated by the code being tested, and returns an object that exposes a set of matcher functions.&lt;br&gt;
+&lt;br&gt;
toBe() is a matcher that performs a strict equality test between the value being tested (the expect() argument) and the expected value (its own argument).&lt;br&gt;
&lt;strong&gt;or&lt;/strong&gt;&lt;br&gt;
The toEqual() assertion method, which checks for deep object equality&lt;/p&gt;

&lt;p&gt;Implemented like so:&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="c1"&gt;//Palindromes.test.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Palindromes&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;./Palindromes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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="s2"&gt;palindromes()&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="s2"&gt;correctly identifies one-word palindromes&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Palindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;madam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;madam&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Palindromes.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Palindromes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="nx"&gt;str&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;To run the test, write following command-line in the command prompt.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Let's get down to the nitty-gritty stuff
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Prettier&lt;/strong&gt;&lt;br&gt;
 commands for code formatting packages&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm i -D prettier&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;npm i -D eslint-config-prettier&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Files needed to be created&lt;/strong&gt;&lt;br&gt;
.prettierignore -&amp;gt; Here we will put files that it should ignore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;node_modules&lt;/li&gt;
&lt;li&gt;coverage&lt;/li&gt;
&lt;li&gt;build&lt;/li&gt;
&lt;li&gt;.vscode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;.prettierrc.json -&amp;gt; the rules for our formatting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"trailingComma"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"es5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"tabWidth"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"semi"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"singleQuote"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"useTabs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"jsxSingleQuote"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"bracketSpacing"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"jsxBracketSameLine"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"arrowParens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"always"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"htmlWhitespaceSensitivity"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"css"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"printWidth"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"quoteProps"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"consistent"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pre-commit hook&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Running a command before comitting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pre-commit hooks is used for&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Running prettier&lt;/li&gt;
&lt;li&gt;Running tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;install a pre-commit hook for prettier, husky and lint-staged&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npx mrm lint-staged&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;add "prettier": "prettier --write ." to scripts or use "prettier --write src/" to only format files in the src folder etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By running &lt;code&gt;npm run prettier&lt;/code&gt;, we can now manually prettify the files.&lt;/p&gt;

&lt;p&gt;install husky folder&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npx husky install&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;install a pre-commit hook for our tests, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npx husky add .husky/pre-commit "npm test"&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;add npm run prettier to the pre-commit file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Install cross-env&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;npm i -D cross-env&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;in package.json: &lt;code&gt;"test": "cross-env CI=true react-scripts test"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's it!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we have successfully made an automated testing and formatting workspace, which will run every time we try and commit&lt;br&gt;
(it will only commit if it passes the test). &lt;br&gt;
This will eliminate unreadable code and committing code with bugs.&lt;/p&gt;




&lt;p&gt;I will leave you with a quote from Trevor Burnham&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In the long term, the habit of putting tests first helps to form a healthy mindset for problem solving, one in which failing code evokes curiosity instead of despair.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;To learn more go visit:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://typicode.github.io/husky/#/"&gt;Husky Webpage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://prettier.io/"&gt;Prettier Webpage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jestjs.io/"&gt;Jest Webpage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.com/Test-Driven-React-Problems-Quickly-Confidence/dp/1680506463/ref=sr_1_2?dchild=1&amp;amp;keywords=test+driven+react&amp;amp;qid=1613924310&amp;amp;sr=8-2"&gt;TDD Book by Trevor Burnham&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>react</category>
      <category>jest</category>
    </item>
  </channel>
</rss>
