02 / 12
Vitest, jsdom ve React Testing Library kurulumu.
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.
$ 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
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"],
},
});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
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"]
}
}{
"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/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ı euro olarak biçimlendiren bir fonksiyon yazın. Dosya: src/format-price.ts.
export function formatPrice(price: number): string {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "EUR",
}).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("shows the product name and price", () => {
render(
<ProductCard
product={{ id: 1, name: "Wireless Headphones", price: 1299, inStock: true }}
/>,
);
expect(
screen.getByRole("heading", { name: "Wireless Headphones" }),
).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ı node olarak kalmış. environment: "jsdom" ayarını ekleyin.
Invalid Chai property: toBeInTheDocument: Setup dosyası yüklenmemiş. setupFiles yolunu kontrol edin.
TypeScript toBeInTheDocument için hata verir: types listesine @testing-library/jest-dom ekleyin.
Cannot find module '@testing-library/dom': Peer dependency eksik. Paketi kurun.
globals: false kullanırsanız her test dosyasında test, expect ve vi fonksiyonlarını vitest paketinden import edin. React Testing Library'nin otomatik temizliği global afterEach fonksiyonuna bağlıdır. Bu durumda setup dosyasında afterEach(() => cleanup()) çağırın.4
Next.js projelerinde Vitest, Client Component ve senkron Server Component testleri için çalışır. async Server Component'ler için E2E test kullanın.9
src/format-price.test.ts dosyası oluşturun.
formatPrice(0) ifadesinin €0.00 dö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.
Dersin ana fikirlerini üç soruyla kontrol edin.
İlerlemeniz yalnızca bu tarayıcıda saklanır.
Son güncelleme tarihi: 3 Kasım 2026