02 / 12
Kurulum
Vitest, jsdom ve React Testing Library kurulumu.
Gereksinimler
Node.js 22.12 veya üstü.
Vite 6.4 veya üstü.
React 18 veya 19.
Bu gereksinimler Vitest 5 içindir.1 Projeniz Vite ile oluşturulduysa ek bir ayar gerekmez.
Paketleri kurun
$ pnpm add -D vitest jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom @testing-library/user-event| Paket | Ne için |
|---|---|
vitest | Test runner |
jsdom | DOM ortamı |
@testing-library/react | render, screen ve renderHook |
@testing-library/dom | React Testing Library'nin peer dependency paketi |
@testing-library/jest-dom | DOM matcher'ları |
@testing-library/user-event | Kullanıcı etkileşimi |
Uyarı
@testing-library/dom paketini ayrıca kurun. React Testing Library bu paketi kendisi kurmaz.2
Vitest ayarı
vite.config.ts dosyasına bir test bloğu ekleyin. defineConfig fonksiyonunu vitest/config paketinden alın.
import react from "@vitejs/plugin-react";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [react()],
test: {
environment: "jsdom",
globals: true,
setupFiles: ["./src/vitest.setup.ts"],
},
});Setup dosyası
src/vitest.setup.ts dosyasını oluşturun:
import "@testing-library/jest-dom/vitest";Bu satır toBeInTheDocument gibi matcher'ları Vitest'e ekler.6
TypeScript tipleri
tsconfig.app.json dosyasındaki types listesine iki paket ekleyin. Projenizde tek bir tsconfig.json varsa o dosyayı kullanın.4
{
"compilerOptions": {
"types": ["vite/client", "vitest/globals", "@testing-library/jest-dom"]
}
}Script'ler
{
"scripts": {
"test": "vitest",
"test:run": "vitest run"
}
}| Komut | Davranış |
|---|---|
npm test | Watch mode. Dosya değişince ilgili testler tekrar çalışır.7 |
npm run test:run | Testleri bir kez çalıştırır. CI ortamında bunu kullanın. |
Vitest, rapor dosyalarını .vitest klasörüne yazar. Bu klasörü .gitignore dosyasına ekleyin:1
.vitest/İlk test
E-ticaret örneğimiz için önce Product tipini oluşturun. Dosya: src/types.ts.
export type Product = {
id: number;
name: string;
price: number;
inStock: boolean;
};Fiyatı Türk lirası olarak biçimlendiren bir fonksiyon yazın. Dosya: src/format-price.ts.
export function formatPrice(price: number): string {
return new Intl.NumberFormat("tr-TR", {
style: "currency",
currency: "TRY",
}).format(price);
}Ürün adını ve fiyatını gösteren bir component yazın. Dosya: src/ProductCard.tsx.
import { formatPrice } from "./format-price";
import type { Product } from "./types";
export function ProductCard({ product }: { product: Product }) {
return (
<article>
<h2>{product.name}</h2>
<p>{formatPrice(product.price)}</p>
</article>
);
}Şimdi testi yazın. Dosya: src/ProductCard.test.tsx.
import { render, screen } from "@testing-library/react";
import { ProductCard } from "./ProductCard";
test("ürün adını ve fiyatını gösterir", () => {
render(
<ProductCard
product={{ id: 1, name: "Kablosuz Kulaklık", price: 1299, inStock: true }}
/>,
);
expect(
screen.getByRole("heading", { name: "Kablosuz Kulaklık" }),
).toBeInTheDocument();
expect(screen.getByText("₺1.299,00")).toBeInTheDocument();
});Testi çalıştırın:
npm run test:runTest geçerse kurulum tamamdır. Vitest, adında .test. veya .spec. geçen dosyaları çalıştırır.8 Test dosyasını, test ettiğiniz dosyanın yanına koyun.
document is not defined: Test ortamınodeolarak kalmış.environment: "jsdom"ayarını ekleyin.Invalid Chai property: toBeInTheDocument: Setup dosyası yüklenmemiş.setupFilesyolunu kontrol edin.TypeScript
toBeInTheDocumentiçin hata verir:typeslistesine@testing-library/jest-domekleyin.Cannot find module '@testing-library/dom': Peer dependency eksik. Paketi kurun.
globals: falsekullanırsanız her test dosyasındatest,expectvevifonksiyonlarınıvitestpaketinden import edin. React Testing Library'nin otomatik temizliği globalafterEachfonksiyonuna bağlıdır. Bu durumda setup dosyasındaafterEach(() => cleanup())çağırın.4Next.js projelerinde Vitest, Client Component ve senkron Server Component testleri için çalışır.
asyncServer Component'ler için E2E test kullanın.9
Kendin dene
src/format-price.test.tsdosyası oluşturun.formatPrice(0)ifadesinin₺0,00döndürdüğünü test edin.Beklenen değeri bilerek bozun. Testin başarısız olduğunu görün.
Beklenen değeri düzeltin. Testin geçtiğini görün.
Kaynaklar
Vite ve Node gereksinimleri, kurulum komutu ve `.vitest` klasörü.
`@testing-library/react` kurulumu ve `@testing-library/dom` peer dependency'si.
Varsayılan `node` ortamı ve jsdom'un ayrıca kurulması.
`globals` ayarı, `vitest/globals` tipi ve React Testing Library'nin otomatik temizliği.
Setup dosyalarının her test dosyasından önce çalışması.
Vitest ile `@testing-library/jest-dom/vitest` import'u.
Watch mode'da yalnızca ilgili testlerin yeniden çalışması.
Varsayılan test dosyası deseni: `.test.` ve `.spec.`.
`async` Server Component'lerin Vitest ile desteklenmemesi ve E2E önerisi.
Son güncelleme tarihi: 14 Ekim 2026
