<?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: Saif Bechan</title>
    <description>The latest articles on Forem by Saif Bechan (@saifbechan).</description>
    <link>https://forem.com/saifbechan</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%2F886450%2F1c521440-19bf-4978-8fd6-a5c45e375497.jpeg</url>
      <title>Forem: Saif Bechan</title>
      <link>https://forem.com/saifbechan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saifbechan"/>
    <language>en</language>
    <item>
      <title>Set up NextJS 12 with Typescript, Chakra UI, ESLint, Prettier, React Testing Library, Vitest and Playwright in VSCode in 2022</title>
      <dc:creator>Saif Bechan</dc:creator>
      <pubDate>Sun, 03 Jul 2022 10:56:37 +0000</pubDate>
      <link>https://forem.com/saifbechan/set-up-nextjs-12-with-typescript-chakra-ui-eslint-prettier-react-testing-library-vitest-and-playwright-in-vscode-in-2022-5eaf</link>
      <guid>https://forem.com/saifbechan/set-up-nextjs-12-with-typescript-chakra-ui-eslint-prettier-react-testing-library-vitest-and-playwright-in-vscode-in-2022-5eaf</guid>
      <description>&lt;h2&gt;
  
  
  What will we cover
&lt;/h2&gt;

&lt;p&gt;We are going to set up a full NextJS stack including code quality and testing tools. We can use this boilerplate to developer high performance frontends.&lt;/p&gt;

&lt;h3&gt;
  
  
  Base
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;VSCode Plugins + Settings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NextJS 12 (React, Typescript)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chakra UI (React UI Framework)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code Quality
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;ESLint (Check for code quality and best practices)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prettier (Format our code so it looks nice)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Unit/Component Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vitest (Lightweight and fast testing framework)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React Testing Library (Used to make testing React components easy)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  E2E Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Playwright (Framework for E2E testing)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Git Hooks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Husky (Makes it easy to work with git hooks)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lint-staged (Lets us only run linting on the files we commit)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;You probably know this by now, we do need some apps get us started. These would be VSCode (😂 duh!), NodeJS and Yarn.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating our project
&lt;/h2&gt;

&lt;p&gt;Keeping this simple. For a more in-depth explanation your best bet would be to check out the NextJS website itself &lt;a href="https://nextjs.org/docs/getting-started"&gt;https://nextjs.org/docs/getting-started&lt;/a&gt;. We are going to dig straight in.&lt;/p&gt;

&lt;p&gt;For the application we are going to use Yarn. There are a few other options you could use like npm or pnpm. There are some differences but they are very minimal. It boils down to personal preference if you ask me.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn create next-app --typescript myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;You should replace “myapp” with the name of your application.&lt;/p&gt;

&lt;p&gt;Out of the box this comes already shipped with a lot of goodies we need. We have a dev environment and a basic setup of ESLint. The only thing we need to do is add some of our own tools and make this work nicely in VSCode.&lt;/p&gt;

&lt;p&gt;Now, let’s open the folder we just created in VSCode and continue with the next steps&lt;/p&gt;
&lt;h2&gt;
  
  
  VSCode Plugins
&lt;/h2&gt;

&lt;p&gt;First of all we want to add some plugins to VSCode. This will allow us to have code formatting while we are working coding instead of just only running the linters via the Terminal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;ESLint&lt;br&gt;
Description: Integrates ESLint JavaScript into VS Code.&lt;br&gt;
VS Marketplace Link: &lt;a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint"&gt;https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prettier — Code formatter&lt;br&gt;
Description: Code formatter using prettier&lt;br&gt;
VS Marketplace Link: &lt;a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode"&gt;https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just these two are fine for now. Later we will add some more optional plugins that will make our coding experience nicer.&lt;/p&gt;
&lt;h2&gt;
  
  
  Set up Prettier to work with ESLint
&lt;/h2&gt;

&lt;p&gt;First we are going to add the needed packages to our project and the continue to configure.&lt;/p&gt;
&lt;h3&gt;
  
  
  Install
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D prettier eslint-config-prettier eslint-plugin-prettier
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Settings
&lt;/h3&gt;

&lt;p&gt;We can have a look at the Prettier documentation to see what kind of settings we want to use. These are not set in stone and can be different per project.&lt;/p&gt;

&lt;p&gt;Now let’s create a file in the root of our project .prettierrc.js with the following code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In your project root you should already have a file that is called .eslintrc.json create by the nextJS install. We just need to edit this slightly to integrate prettier.&lt;br&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Basically what this does is telling ESLint to also show prettier errors. This way we can only use one command.&lt;/p&gt;

&lt;p&gt;To make our life just a little easier we can add the following line of code to the scripts section of our package.json file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"lint:fix": "next lint --fix",
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And we can run it.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn lint:fix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  VSCode settings
&lt;/h2&gt;

&lt;p&gt;Next, let’s add set up some settings so we have formatting while we are coding.&lt;/p&gt;

&lt;p&gt;Let’s create the file .vscode/settings.json in the root of our project and paste the following code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Implement Chakra UI
&lt;/h2&gt;

&lt;p&gt;This step is completely optional. A good UI framework just helps out a lot and Chakra UI is a good option. It has a lot of options, highly flexible, they are up to date and there is good community support.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Just to make sure everything is working correctly let’s add some Chakra UI components.&lt;/p&gt;

&lt;p&gt;We can create/edit these files in the /pages/ directory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;_app.tsx&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;about.tsx&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;index.tsx&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Implement Vitest &amp;amp; React Testing Framework
&lt;/h2&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since React is such a populair framework there are a lot of options available. Vitest is a new framework that is lightweight and fast. One of the nice things is that we do not need babel to run.&lt;/p&gt;

&lt;p&gt;Vitest will only run the tests for us but we also need a library that can render the react components. For this we will use React Testing Library.&lt;/p&gt;

&lt;p&gt;Let’s get the libraries installed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D vitest @vitejs/plugin-react @testing-library/react jsdom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Next we can add the config for vitest. React Testing Library does not need any additional config.&lt;/p&gt;

&lt;p&gt;Let’s create the file vitest.config.ts in the root of the project and add the following code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
We can now add a simple test for the homepage. We have a convention to add the tests inside a folder called &lt;code&gt;__test__&lt;/code&gt; so let’s go ahead and do that.&lt;/p&gt;

&lt;p&gt;__tests__/Home.test.tsx&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Finally in our package.json in the scripts section we can add the following lines.

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"test": "vitest",
"test:ci": "vitest run",
"coverage": "vitest run --coverage",
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Run the test&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn test:ci
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Implement Playwright
&lt;/h2&gt;

&lt;p&gt;For integration or end-to-end testing we will use a framework called Playwright. This framework is also somewhat new and comes packed with a lot of nice features.&lt;/p&gt;

&lt;p&gt;Let’s install.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D @playwright/test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;In the root of our project we can add the file playwright.config.ts with the following content.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
In the scripts section of our package.json we can put the following code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"test:e2e": "playwright test",
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The convention is to create a folder inside the root of our project names e2e where we place our integration tests.&lt;/p&gt;

&lt;p&gt;Let’s create a home.spec.ts inside this folder.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now run the test.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn test:e2e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Git Hooks
&lt;/h2&gt;

&lt;p&gt;Now even though our code gets already linted and prettified while we are typing it is always good to run a check before we commit. This way we make sure that every time we commit something it is in the correct format.&lt;/p&gt;

&lt;p&gt;We will use two tools for this. The first one is husky, which will make creating git hooks a little easier. The next tool is lint-staged which will make sure we only run the linters on files we actually changed. If your codebase grows it might take a long time to run linters on the whole project. If you just changed one line we can run the linter only on that change.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D lint-staged husky
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Next we create a .lintstagedrc.js file in the root of our project.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
After this to set it up we have to run the husky install script.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn husky install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We also have to make sure to again add a line to the scripts section of our package.json file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
"prepare": "husky install",
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now you will have a .husky folder in the root of your project. In this folder we can add the following two files. They are just called pre-commit and pre-push respectively. As the name suggests one will run before we commit and the other one before we push.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx husky add .husky/pre-commit "yarn lint-staged"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above script will run the linter on all our staged files before we commit.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx husky add .husky/pre-push "yarn lint"
npx husky add .husky/pre-push "yarn test:ci"
npx husky add .husky/pre-push "yarn test:e2e"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This final script will run linters, unit tests, and integration tests before we push our code. This way we can make sure our CI/CD pipelines do not get stuck.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Repo
&lt;/h2&gt;

&lt;p&gt;The final result of this all is available on Github.&lt;br&gt;
&lt;a href="https://github.com/saifbechan/nextjs-ts-eslint-chakraui-vitest-playwright"&gt;&lt;strong&gt;GitHub - saifbechan/nextjs-ts-eslint-chakraui-vitest-playwright&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>vitest</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
