Skip to content

vite-react-ts-starter with eslint, prettier, vitest, testing library and react router setup

License

Notifications You must be signed in to change notification settings

keremaydemir123/vite-react-ts-starter

Repository files navigation

vite-react-ts-starter

Create React-Ts App

  1. npm create vite@latest
  2. clear unnecessary files (index.css favicons etc.)

Eslint Setup

  1. npm i -D eslint
  2. npx install-peerdeps --dev eslint-config-airbnb
  3. in .eslintrc.cjs find "extends" array, clear "eslint:recommended" and add "airbnb", "airbnb/hooks"

it should be look like :

  extends: [
    "airbnb",
    "airbnb/hooks",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
  ],
  1. npm i -D eslint-config-airbnb-typescript
  2. in .eslintrc.cjs find "extends" array, add "airbnb-typescript"

it should be like this:

  extends: [
    "airbnb",
    "airbnb-typescript",
    "airbnb/hooks",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
  ],
  1. in .eslintrc.cjs find "parserOptions" object, add project: './tsconfig.json'
  2. in .eslintrc.cjs find "rules" array, add 'react/react-in-jsx-scope':0

Prettier Setup

  1. npm i -D prettier eslint-config-prettier eslint-plugin-prettier
  2. create .prettierrc.cjs at root folder
  3. add those lines:
module.exports = {
    trailingComma: 'es5',
    tabWidth: 2,
    semi: true,
    singleQuote: true,
};
  1. in .eslintrc.cjs find "plugins" array, add 'prettier'
  2. in .eslintrc.cjs find "extends" array, add 'plugin:prettier/recommended' to the LAST LINE to avoid conflicts with eslint

it should be look like:

  extends: [
    'airbnb',
    'airbnb-typescript',
    'airbnb/hooks',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended',
  ],

Testing Setup

  1. npm install -D vitest
  2. refactor vite.config.ts to be look like this:
/* eslint-disable import/no-extraneous-dependencies */
/// <reference types="vitest" />
/// <reference types="vite/client" />

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['./src/setupTests.ts'],
  },
});
  1. in tsconfig.json file, find "include" array, add 'vite.config.ts'
  2. npm install --D @testing-library/react @testing-library/jest-dom
  3. create setupTests.ts inside /src folder and add those code:
/* eslint-disable import/no-extraneous-dependencies */
import matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';

expect.extend(matchers);
  1. create App.test.tsx for first test
  2. add those lines to check if DOM has a text with "Hello Word"
import { describe, it } from 'vitest';
import { render, screen } from '@testing-library/react';

import App from './App';

describe('App', () => {
  it('Renders hello world', () => {
    // ARRANGE
    render(<App />);
    // ACT
    // EXPECT
    expect(
      screen.getAllByRole('heading', {
        level: 1,
      })
    ).toHaveTextContent('Hello Word');
  });
});
  1. in package.json find "scripts" object, add "test": "vitest"
  2. to run tests now you can type npm test
  3. You can check queries for testing library from here

React Router Setup

  1. npm install react-router-dom@6
  2. in App.tsx add React Router imports and modify the file as:
import { HashRouter, Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import NotFound from './pages/NotFound';

export function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}

export function WrappedApp() {
  return (
    <HashRouter>
      <App />
    </HashRouter>
  );
}
  1. in /src folder, create pages folder.
  2. create your "Home" and "NotFound" components here

About

vite-react-ts-starter with eslint, prettier, vitest, testing library and react router setup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published