Setting up a development environment is an essential step for any software project. It ensures that all the necessary tools and dependencies are in place to maximize productivity and maintain code quality. In this article, we will walk you through the process of setting up Vite, React, TypeScript, ESLint, Prettier, Vitest, testing-library, and React Router, which are popular tools used in modern web development.
Vite: Vite is a fast, opinionated web dev build tool that focuses on providing an instant development experience. It leverages the native browser ES module system, which allows for faster hot module replacement and optimizations during development.
React: React is a popular JavaScript library for building user interfaces. It offers a component-based approach to website development, making it easier to manage complex UIs.
TypeScript: TypeScript is a superset of JavaScript that adds static typing to the language. It helps catch errors at compile-time, provides better tooling support, and improves code maintainability and scalability.
ESLint: ESLint is a pluggable linting utility for JavaScript and TypeScript. It helps enforce coding standards, identify potential bugs, and improve code quality by highlighting problematic patterns and enforcing best practices.
Prettier: Prettier is a code formatter that automatically formats your code according to a set of predefined rules. It removes the need for manual formatting, reduces conflicts in team collaboration, and promotes consistent coding styles.
Vitest: Vitest is a lightweight testing framework specifically made for Vite. It provides a simple, intuitive API for testing React components and hooks.
Testing-Library: Testing-Library is a set of utilities for testing JavaScript code. It provides a user-centric approach to testing by focusing on simulating user interactions and asserting their effects rather than testing implementation details.
React Router: React Router is a popular library for managing routing in React applications. It enables navigation and routing within a single-page application, allowing for a more seamless user experience.
Now, let’s move on to the steps of setting up these tools:
Step 1: Creating a new project
To start, create a new project directory and navigate to it in your terminal.
Step 2: Initializing a new project
Use the following command to initialize a new project with npm:
“`bash
npm init -y
“`
This will create a `package.json` file with default configurations.
Step 3: Installing dependencies
Now it’s time to install the required dependencies. Run the following command:
“`bash
npm install vite react react-dom react-router-dom typescript eslint prettier vitest @testing-library/react –save-dev
“`
Step 4: Configuring Vite
Create a `vite.config.ts` file in the project’s root directory. Add the following content to configure Vite to use React and TypeScript:
“`javascript
import { defineConfig } from ‘vite’;
import react from ‘@vitejs/plugin-react’;
export default defineConfig({
plugins: [react()],
});
“`
Step 5: Configuring ESLint
Create an `.eslintrc.json` file in the project’s root directory. Add the following content to configure ESLint:
“`json
{
“extends”: [“react-app”, “plugin:@typescript-eslint/recommended”],
“parserOptions”: {
“project”: “./tsconfig.json”
},
“rules”: {
// Add custom rules here if needed
},
“ignorePatterns”: [“dist/”, “node_modules/”]
}
“`
Step 6: Configuring Prettier
Create a `.prettierrc.json` file in the project’s root directory. Add the following content to configure Prettier:
“`json
{
“semi”: true,
“singleQuote”: true,
“printWidth”: 80,
“tabWidth”: 2
}
“`
Step 7: Configuring TypeScript
Create a `tsconfig.json` file in the project’s root directory. Add the following content to configure TypeScript:
“`json
{
“compilerOptions”: {
“target”: “es6”,
“module”: “esnext”,
“jsx”: “react”,
“strict”: true,
“esModuleInterop”: true,
“skipLibCheck”: true,
“forceConsistentCasingInFileNames”: true
}
}
“`
Step 8: Writing your first React component and test
Create a new React component in a file called `App.tsx`. Add some JSX content to it.
Create a new test file called `App.test.tsx` and write a sample test using Vitest and Testing-Library.
Step 9: Running the development server
In your terminal, run the following command to start the Vite development server:
“`bash
npm run dev
“`
This will start the development server, and you should see your React component being rendered in the browser.
Step 10: Running tests
In another terminal, run the following command to run your tests:
“`bash
npm run test
“`
This will execute your test cases and provide feedback about their success or failure.
Congratulations! You have successfully set up Vite, React, TypeScript, ESLint, Prettier, Vitest, testing-library, and React Router in your development environment. You are now equipped with a powerful toolset for developing high-quality, scalable web applications. Happy coding!
very usefull video thank you for makin it. I have question I feel like I am following each steps one by one but somehow I still can manage to miss something or I dunno at some point I have a different result than the video. Can it be because now the codes changes a little or my previous setting are not allowing it or I am really missing something.
Who else had a same issue ?
Nice overview, thank you!
OMG! I tryied to set up jets for 2 days and now I found this video, thank you a lot!
Anyone or @CodingGarden having a problem with the App.test.tsx file?
I'm getting "Cannot find name 'expect'" – when I import it from 'vitest' I get a different error:
Property 'toHaveTextContent' does not exist on type 'Assertion<HTMLElement>'
No idea what I'm missing here.
All steps have been followed perfectly excluding the fact that I converted this app from CRA to Vite before starting this project…
Thank you CJ for the super helpful video.
If someone get error when accessing `toHaveTextContent` when writing tests add `import '@testing-library/jest-dom/vitest' ` in setupTest.ts file
Thank you for this guide!
I found answers to some questions I had before.
If anyone runs into an error (related to expect.extend(matchers);) in your jest setup upon running a test, change your matchers import to "import * as matchers from '@testing-library/jest-dom/matchers';"
where is placed expect() function
I have been updating a react app that I 'inherited' and decided to use vite, vitest and eslint. I read through countless articles, all saying different things, all configuring their apps slightly differently. Then I found this video …. a presenter that speaks clearly, presents the content at a comfortable pace to follow along, doesn't rush or skip over important bits. One of the best coding tutorials I have come across. Thanks to this video, i was easily able to re-configure the app. Great job.
Thanks, helped me a lot
comma dangle, ick!
rules: {
'react/react-in-jsx-scope': 0,
"comma-dangle": "off",
"@typescript-eslint/comma-dangle": "off"
},
7:37 : Looks like the latest vite installer adds eslint by default.
2:04 … By naming it react-ts-app it automatically created tsconfig for you and you weren't prompted if you wanted typescript or not? what sort of magic is this?
But what if I need to setup react app vite with only RTL, I mean without using vitest? How to bootstrap that in the config files?
Thanks, I was struggling so much with the setup, you made it really easy
Great stuff! Been slinging code for almost 20 years picking up TypeScript and React after working in ServiceNow for the last 5 years. Used React back in the day for a project didn't like but I am seeing some good changes they have done, plus a huge fan of Nextjs SSR. It is actually the 3As of testing, Arrange, Act , Assert , not expect. Also I see you putting the Act in the Assert line usually I separate them out to have 3 distinct lines. Hopefully soon vite cli will be able to pick testing libraries like it is in nextjs with npx. Vite works great for Vue 3 apps as well, always like Vue. Keep up the good work, wish this all existed back when I was cutting my teeth, now the problem is having the time to watch the 60 tabs of videos. To all the self educators trying to get a start I was framing houses while in school, keep grinding and you will get there! Build stuff everyday and hustle up some freelance work! It landed me my first fulltime gig.
thank you
thank you for this video, really appreciate you taking the time to walk through everything! I'm working through The Odin Project and am currently on the React testing lessons. They teach you how to use create-react-app to build applications (which has Jest and the react testing library configured out of the box), but I much prefer Vite. This video includes everything that I need to know to still follow the curriculum despite the differences in tech used.
sir i love your turorial am your follower from cameroon
When I converted all my jest based tests to vitest, It took a looong time to execute 😢