{"id":400,"date":"2019-06-18T02:49:11","date_gmt":"2019-06-18T02:49:11","guid":{"rendered":"https:\/\/jmrowe.com\/blog\/?p=400"},"modified":"2019-06-18T03:11:51","modified_gmt":"2019-06-18T03:11:51","slug":"setup-codesandbox-io-jest-to-work-with-react-components-for-testing","status":"publish","type":"post","link":"https:\/\/jmrowe.com\/blog\/setup-codesandbox-io-jest-to-work-with-react-components-for-testing\/","title":{"rendered":"Setup Codesandbox.io + Jest to work with React Components for testing."},"content":{"rendered":"<p>First step is create a new sandbox with the create-react-app base. Then add these three dependencies to your sandbox.<\/p>\n<p>enzyme &amp; enzyme-adapter-react-16 &amp; <span style=\"font-size: 1rem;\">@testing-library\/react<\/span><\/p>\n<p>The enzyme-adapter-react-16 name will slightly change with newer releases of react.<\/p>\n<p>Next, create a file in your src folder and name it setupTests.js . This files goves jest with the ability to understand React.<\/p>\n<pre class=\"lang:js decode:true\">\/\/ setTest.js\r\n\r\nimport Enzyme from \"enzyme\";\r\nimport Adapter from \"enzyme-adapter-react-16\";\r\n\r\n\/\/ React 16 Enzyme adapter, at the time of this writing.\r\nEnzyme.configure({ adapter: new Adapter() });\r\n<\/pre>\n<p>Also, insert this in to your package.json file.<\/p>\n<pre class=\"lang:js decode:true \"> \"jest\": {\r\n    \"setupTestFrameworkScriptFile\": \"&lt;rootDir&gt;\/src\/setupTests.js\"\r\n  }<\/pre>\n<p><a href=\"https:\/\/jmrowe.com\/blog\/development\/javascript\/setup-codesandbox-io-jest-to-work-with-react-components-for-testing\/attachment\/untitled-2\" rel=\"attachment wp-att-411\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-411\" src=\"https:\/\/jmrowe.com\/blog\/wp-content\/uploads\/2019\/06\/Untitled-1.png\" alt=\"\" width=\"589\" height=\"365\" srcset=\"https:\/\/jmrowe.com\/blog\/wp-content\/uploads\/2019\/06\/Untitled-1.png 589w, https:\/\/jmrowe.com\/blog\/wp-content\/uploads\/2019\/06\/Untitled-1-300x186.png 300w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/a><\/p>\n<p>Now, for our test scenario we will create and use a functional component ( using hooks ) that has 1 input ( that will use useState ) and 1 prop to use and test.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First step is create a new sandbox with the create-react-app base. Then add these three dependencies to your sandbox. enzyme &amp; enzyme-adapter-react-16 &amp; @testing-library\/react The enzyme-adapter-react-16 name will slightly change with newer releases of react. Next, create a file in your src folder and name it setupTests.js . This files goves jest with the ability [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-400","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":12,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts\/400\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/tags?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}