React template with TypeScript, Vite, Styled Components, and testing setup.
- Node.js (version 18 or higher)
- npm or yarn
-
Clone the repository:
git clone https://github.com/tyreer/react-ts-template.git cd dev-template -
Install dependencies:
npm install
-
Start development server:
npm run dev
-
Run tests:
npm test -
Build for production:
npm run build
- React 19 - Latest React with modern features
- Vite - Lightning-fast build tool and dev server
- TypeScript - Full type safety and IntelliSense
- Styled Components - CSS-in-JS with TypeScript support
- Vitest - Fast unit testing framework
- React Testing Library - Simple and complete testing utilities
- ESLint - Code linting and formatting
- Prettier - Code formatting with import sorting
react&react-dom- React frameworkstyled-components- CSS-in-JS styling@types/styled-components- TypeScript definitions
@vitejs/plugin-react- Vite React plugintypescript- TypeScript compilervitest- Testing framework@testing-library/react- React testing utilities@testing-library/jest-dom- Custom Jest matchers@testing-library/user-event- User interaction testingjsdom- DOM environment for testingeslint- Code linting
src/
├── components/ # Reusable UI components
│ ├── Card.tsx # Example card component
│ ├── Card.test.tsx # Card component tests
│ └── index.ts # Component exports
├── test/ # Test configuration
│ └── setup.ts # Test setup file
├── assets/ # Static assets
├── App.tsx # Main application component
├── main.tsx # Application entry point
├── index.css # Global styles
└── vite-env.d.ts # Vite type definitions
This template uses Styled Components for CSS-in-JS styling:
- Styled Components - CSS-in-JS with TypeScript support
- TypeScript Support - Full type safety for styled components
- Component-based Styling - Encapsulated styles per component
- Global Styles - CSS file for global styles and resets
import styled from 'styled-components';
const Container = styled.div`
padding: 1rem;
background-color: #f5f5f5;
`;
const Button = styled.button`
background-color: #007bff;
color: white;
&:hover {
background-color: #0056b3;
}
`;
function App() {
return (
<Container>
<Button>Click me</Button>
</Container>
);
}The project includes a comprehensive testing setup:
- Vitest - Fast test runner with Jest-compatible API
- React Testing Library - Simple and complete testing utilities
- User Event - Realistic user interaction simulation
- JSDOM - Browser-like environment for tests
# Run tests in watch mode
npm test
# Run tests once
npm run test:run
# Run tests with UI
npm run test:uiimport { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Button } from './Button';
test('button handles click events', async () => {
const handleClick = vi.fn();
const user = userEvent.setup();
render(<Button onClick={handleClick}>Click me</Button>);
await user.click(screen.getByRole('button'));
expect(handleClick).toHaveBeenCalledTimes(1);
});npm run dev- Start development servernpm run build- Build for productionnpm run preview- Preview production buildnpm test- Run tests with type checkingnpm run test:run- Run tests once with type checkingnpm run test:ui- Run tests with UInpm run test:only- Run tests without type checkingnpm run lint- Run ESLintnpm run format- Format code with Prettiernpm run format:check- Check code formattingnpm run bootstrap <name>- Create new component with tests
Quickly generate new components with npm run bootstrap <name>. The script auto-converts input to PascalCase and creates component, test, and export files:
npm run bootstrap button # Creates Button component
npm run bootstrap user-profile # Creates UserProfile componentBuilt-in error handling with automatic error boundaries:
- Automatic wrapping - All components (Card and bootstrapped) are automatically wrapped with error boundaries to contain error propagation
- Custom fallback - Override defaults with custom fallback components and onError handlers for component-specific recovery guidance, error logging, etc.
Based on Use react-error-boundary to handle errors in React by Kent C. Dodds.
vite.config.ts- Vite configuration with Vitest setuptsconfig.json- TypeScript configurationvitest.config.ts- Vitest testing configuration.gitignore- Comprehensive git ignore rules