1. React Testing Library 설치

npm install @testing-library/react @testing-library/jest-dom

2. 테스트 환경 설정

npm install vitest @vitejs/plugin-react
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { configDefaults } from "vitest/config";

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "./src/setupTests.ts",
  },
});

import '@testing-library/jest-dom';

테스트 파일 작성

import { render, screen } from "@testing-library/react";
import { test, expect } from "vitest";
import App from "./App";

test("renders App component", () => {
  render(<App />);
  const headingElement = screen.getByText(/hello world/i);
  expect(headingElement).toBeInTheDocument();
});

테스트 실행

npx vitest