<?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>Forem: Trinity Nguyen</title>
    <description>The latest articles on Forem by Trinity Nguyen (@trinwin).</description>
    <link>https://forem.com/trinwin</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%2F424933%2F4b690055-af74-4502-aff1-fb96fb5c1dce.jpg</url>
      <title>Forem: Trinity Nguyen</title>
      <link>https://forem.com/trinwin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/trinwin"/>
    <language>en</language>
    <item>
      <title>Share your Github Profile Readme</title>
      <dc:creator>Trinity Nguyen</dc:creator>
      <pubDate>Sat, 22 Aug 2020 12:49:21 +0000</pubDate>
      <link>https://forem.com/trinwin/share-your-github-profile-readme-51eo</link>
      <guid>https://forem.com/trinwin/share-your-github-profile-readme-51eo</guid>
      <description>&lt;p&gt;I spent 3 hours trying to make it looks good ✌🏻 I'm thinking of how I can improve it 🤔.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here is the final look:&lt;/p&gt;
&lt;/blockquote&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9dvg051apomqvchbdyui.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9dvg051apomqvchbdyui.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👾 Link to my Github: &lt;a href="https://github.com/trinwin" rel="noopener noreferrer"&gt;https://github.com/trinwin&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  What does your Readme look like? ⬇️
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't forget to show the one you live some 😍🌟💫🌟⭐️🥰&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>github</category>
      <category>beginners</category>
      <category>git</category>
      <category>discuss</category>
    </item>
    <item>
      <title>2020 Complete Setup for Storybook, Nextjs, Typescript, SCSS and Jest</title>
      <dc:creator>Trinity Nguyen</dc:creator>
      <pubDate>Tue, 18 Aug 2020 01:12:59 +0000</pubDate>
      <link>https://forem.com/trinwin/2020-complete-setup-for-storybook-nextjs-typescript-scss-and-jest-4khm</link>
      <guid>https://forem.com/trinwin/2020-complete-setup-for-storybook-nextjs-typescript-scss-and-jest-4khm</guid>
      <description>&lt;p&gt;In this article, I will guide you step by step to set up Storybook with Next, Typescript, SCSS, and Jest.&lt;/p&gt;

&lt;p&gt;Storybook is an open-source tool for developing UI components in isolation. It makes building stunning UIs organized and efficient. However, it can be quite tricky to set up with Nextjs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Requirements
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Node.js 10.13 or later&lt;/li&gt;
&lt;li&gt;MacOS, Windows (including WSL), and Linux are supported&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Create Nextjs App
&lt;/h1&gt;

&lt;p&gt;Create a new Next.js app using create-next-app, which sets up everything automatically for you. To create a project, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx create-next-app
✔ What is your project named? … my-app
✔ Pick a template › Default starter app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Enter your project name + hit return&lt;/li&gt;
&lt;li&gt;You will be asked to choose a template: Use arrow key ⬇ to choose a &lt;code&gt;Default starter app&lt;/code&gt; and hit return&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the installation is complete, to start the development server:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-app
yarn run dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You should see this page on localhost:3000&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FHVRFF7Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d16it50kz4mbrdtjlxi0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FHVRFF7Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/d16it50kz4mbrdtjlxi0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  TypeScript
&lt;/h1&gt;

&lt;p&gt;Next, let’s configure Typescript for our Next app&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D typescript @types/react @types/node
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Create a &lt;code&gt;tsconfig.json&lt;/code&gt; in the root folder — this is where you will put your typescript configurations.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
$ touch tsconfig.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And add the following config to the file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Remove &lt;code&gt;index.js&lt;/code&gt; and create &lt;code&gt;index.tsx&lt;/code&gt; file. You can do it manually or use these commands in the root folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
rm -f pages/index.js
touch pages/index.tsx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Add the following to &lt;code&gt;index.tsx&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Restart your server and check out your &lt;code&gt;http://localhost:3000/&lt;/code&gt; by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn run dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WHzlFhzC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/savd3hqjrnphqryhecnx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WHzlFhzC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/savd3hqjrnphqryhecnx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Storybook
&lt;/h1&gt;

&lt;p&gt;Next, we’ll configure Nextjs, SCSS, and Typescript for Storybook&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D @storybook/react @storybook/preset-typescript
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Create &lt;code&gt;.storybook&lt;/code&gt; folder and storybook config files:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
mkdir .storybook
cd .storybook
touch .storybook/main.js .storybook/next-preset.js .storybook/preview.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we will go over how to configure these files.&lt;/p&gt;
&lt;h2&gt;
  
  
  next-preset.js
&lt;/h2&gt;

&lt;p&gt;In this file, we will configure Typescript and SCSS to work with Storybook&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D sass style-loader css-loader sass-loader @babel/core babel-loader babel-preset-react-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Add the following configuration to &lt;code&gt;next-preset.js&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  SCSS
&lt;/h2&gt;

&lt;p&gt;Create your style folder in the root and add global scss file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
mkdir styles
touch styles/global.scss
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  preview.js
&lt;/h2&gt;

&lt;p&gt;In this file, we configure the “preview” iframe that renders your components. We will import your global scss file here.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  main.js
&lt;/h2&gt;

&lt;p&gt;main.js is the most important config file. This is where we place the main configuration of Storybook.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Create a story
&lt;/h2&gt;

&lt;p&gt;Let’s create a simple Button component and a story to test our Storybook setup. First, create a components folder and 2 files &lt;code&gt;Button.tsx&lt;/code&gt; and &lt;code&gt;Button.stories.tsx&lt;/code&gt; in the folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder*/
mkdir components
touch components/Button.tsx components/Button.stories.tsx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then, add the following contents into 2 files:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;




&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, add npm script to &lt;code&gt;package.json&lt;/code&gt; to start storybook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  ...
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "storybook": "start-storybook -p 6006 -c .storybook"
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now, let’s run our Storybook.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn storybook
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VrULRROP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3mh1s0at8pdu1wiv5smk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VrULRROP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3mh1s0at8pdu1wiv5smk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see our global scss style took affect and 2 stories that we have created earlier to test the Button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4IAksEDo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2jm1deca9qk4hs7una4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4IAksEDo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2jm1deca9qk4hs7una4s.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PF-CwB4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/19rtxjqz2si740qgcjag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PF-CwB4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/19rtxjqz2si740qgcjag.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Jest
&lt;/h1&gt;

&lt;p&gt;Next, we will add &lt;code&gt;unit tests&lt;/code&gt; and &lt;code&gt;snapshot tests&lt;/code&gt; in Jest for testing components in Nextjs and Typescript.&lt;/p&gt;

&lt;p&gt;First, let’s install these development dependencies for Jest.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D jest @types/jest ts-jest babel-jest @types/enzyme enzyme enzyme-adapter-react-16
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We’ll need to configure Enzyme to use the adapter, which we can do in Jest’s bootstrap file. Let’s create a config folder and place the setup file in there.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
mkdir config
touch config/setup.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This code will run also before each test but after the testing framework gets executed:&lt;/p&gt;

&lt;p&gt;Now let’s create a config file for jest. If you place your setup file above at a different location then make sure to change your &lt;code&gt;setupFiles: […]&lt;/code&gt; in &lt;code&gt;jest.config.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
$ touch jest.config.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Config babel.config.json
&lt;/h2&gt;

&lt;p&gt;Lastly, we will add babel configurations. Let’s add these dev dependencies to our package.json by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D @babel/preset-env @babel/preset-react @babel/preset-flow @babel/plugin-transform-runtime babel-plugin-transform-es2015-modules-commonjs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In the root folder, create a babel config file. For some reasons, babel.rc does not work and I have to replace it with &lt;code&gt;babel.config.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
$ touch babel.config.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Let’s create a test
&lt;/h2&gt;

&lt;p&gt;Now, let’s run a simple unit test to test the index file that we created earlier to make sure that it has the welcome message “Welcome to My Next App!” as a “h1” element.&lt;/p&gt;

&lt;p&gt;First, create a &lt;code&gt;__test__&lt;/code&gt; folder to keep our test files in one place and create &lt;code&gt;index.test.tsx&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* root folder */
mkdir components/__test__
touch components/__test__/index.test.tsx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Snapshot testing
&lt;/h2&gt;

&lt;p&gt;Finally, I will show you how to create a simple snapshot test. We use Snapshot testing to keep a copy of the structure of the UI component or a snapshot so when after we make any changes we can review the changes and update the snapshots. You can read more about Snapshot testing here.&lt;/p&gt;

&lt;p&gt;To start, let’s install react-test-renderer, a library that enables you to render React components as JavaScript objects without the need for a DOM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add -D react-test-renderer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now, create a file called &lt;code&gt;Button.snapshot.test.tsx&lt;/code&gt; to test create new snapshots for the Button component.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ touch components/__test__/Button.snapshot.test.tsx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now, add the add npm script to &lt;code&gt;package.json&lt;/code&gt; to run your tests&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  ...
  "scripts": {
    ...
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage"
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Go ahead and run your tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn run test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see 1 unit test and 1 snapshot test are passed&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XBFd-nqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eix824pazd836e9u7v7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XBFd-nqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eix824pazd836e9u7v7a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run into errors such as “&lt;code&gt;The default export is not a React Component in page: ‘/’&lt;/code&gt;” or “&lt;code&gt;ReferenceError: regeneratorRuntime is not defined&lt;/code&gt;”, try to delete &lt;code&gt;package-lock.json&lt;/code&gt;, &lt;code&gt;node_modules&lt;/code&gt; folder, and &lt;code&gt;.next&lt;/code&gt; folder and then restart your server, storybook and rerun your test again.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Thank you for reading 🙏🏻 and let me know in the comment if you run into any problems and if it's helpful for you.&lt;/p&gt;

&lt;p&gt;You can also clone the source code here to get started on your development right away: &lt;a href="https://github.com/trinwin/storybook-next-ts-template"&gt;https://github.com/trinwin/storybook-next-ts-template&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Connect with me on &lt;a href="https://medium.com/@trinwin"&gt;Medium&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/trinwin"&gt;LinkedIn&lt;/a&gt;, &lt;a href="https://github.com/trinwin"&gt;Github&lt;/a&gt;, and &lt;a href="https://twitter.com/_trinwin"&gt;Twitter&lt;/a&gt; 🤓.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>tutorial</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
