A modern starter template for building and testing React applications using Vite, TypeScript, Vitest, and React Testing Library. This project is designed for both learning and rapid prototyping, with a focus on best practices for testing React components.
- Project Summary
- Features
- Technology Stack
- Project Structure
- Getting Started
- Available Scripts
- Testing Setup & Instructions
- Teaching Content: React Testing Walkthrough
- Keywords
- License
This template provides a robust environment for developing and testing React applications. It leverages Vite for fast development, TypeScript for type safety, Tailwind CSS for styling, and Vitest with React Testing Library for comprehensive testing. The included teaching content will guide you through setting up and using these tools for effective React testing.
- β‘ Fast development with Vite
- π‘οΈ TypeScript support
- π¨ Tailwind CSS for utility-first styling
- π§ͺ Vitest for unit and component testing
- π§° React Testing Library for testing React components
- π Pre-configured ESLint for code quality
- π Teaching content and step-by-step testing instructions
- React (v18+)
- TypeScript
- Vite
- Vitest
- React Testing Library
- JSDOM
- Tailwind CSS
- ESLint
.
βββ public/
β βββ vite.svg
βββ src/
β βββ App.tsx
β βββ Random.tsx
β βββ main.tsx
β βββ index.css
β βββ vitest.setup.ts
β βββ __tests__/
β βββ App.test.tsx
β βββ Random.test.tsx
βββ package.json
βββ tsconfig.json
βββ tsconfig.app.json
βββ tsconfig.node.json
βββ vite.config.ts
βββ tailwind.config.js
βββ postcss.config.js
βββ eslint.config.js
βββ README.md
- Node.js (v18 or higher recommended)
- npm
-
Clone the repository:
git clone <your-repo-url> cd 02-testing-project-template
-
Install dependencies:
npm install
-
Start the development server:
npm run dev
-
Open your browser: Visit http://localhost:5173/ in your browser.
npm run dev
β Start the Vite development servernpm run build
β Build the app for productionnpm run preview
β Preview the production buildnpm run test
β Run all tests with Vitestnpm run lint
β Lint the codebase with ESLint
This project is pre-configured for React Testing Library and Vitest. See the next section for a detailed walkthrough and teaching content.
You can use it as a starting point for your own projects.
A brief walkthrough on how to set up Vite (TypeScript Template), Vitest, and React Testing Library.
-
Create a new Vite project (React + TypeScript):
npm create vite@latest
-
Install Vitest:
npm install vitest --save-dev
-
Add the test script to your
package.json
:"scripts": { "test": "vitest" }
-
Create a test file, e.g.,
random.test.ts
:import { describe, it, expect } from "vitest"; describe("basic arithmetic checks", () => { it("1 + 1 equals 2", () => { expect(1 + 1).toBe(2); }); it("2 * 2 equals 4", () => { expect(2 * 2).toBe(4); }); });
-
Run the test:
npm run test
-
Install React Testing Library and dependencies:
npm install @testing-library/react @testing-library/jest-dom jsdom @testing-library/user-event --save-dev
@testing-library/react
β Core testing utilities for React components@testing-library/jest-dom
β Custom matchers to simplify assertionsjsdom
β Simulates a browser-like environment for tests to run in Node.js@testing-library/user-event
β Simulates user interactions (clicks, typing, etc.) in tests
-
Update
vite.config.ts
:import { defineConfig } from "vitest/config"; import react from "@vitejs/plugin-react"; export default defineConfig({ plugins: [react()], test: { globals: true, environment: "jsdom", setupFiles: "./src/vitest.setup.ts", }, });
-
Create a setup file
src/vitest.setup.ts
:import { expect, afterEach } from "vitest"; import { cleanup } from "@testing-library/react"; import * as matchers from "@testing-library/jest-dom/matchers"; expect.extend(matchers); afterEach(() => { cleanup(); });
-
Update
tsconfig.app.json
to use Vitest globals:{ "compilerOptions": { "types": ["vitest/globals", "@testing-library/jest-dom"] } }
-
Example React component and test:
-
src/Random.tsx
const Random = () => { return <div>Random Component</div>; }; export default Random;
-
src/__tests__/Random.test.tsx
import { describe, it, expect } from "vitest"; import { render, screen } from "@testing-library/react"; import Random from "../Random"; describe("Random Component", () => { it("renders correctly", () => { render(<Random />); screen.debug(); const element = screen.getByText("Random Component"); expect(element).toBeInTheDocument(); }); });
-
-
Run your tests:
npm run test
You should see output indicating your tests have passed.
React, Vite, TypeScript, Vitest, React Testing Library, JSDOM, Tailwind CSS, ESLint, Testing, Unit Test, Component Test, Template, Boilerplate, Teaching, Walkthrough
This project is for educational purposes. See the course or repository for license details.