vitest_runner
Original:🇺🇸 English
Translated
Modern JavaScript/TypeScript testing with Vitest including mocking and coverage.
1installs
Added on
NPX Install
npx skill4agent add vuralserhat86/antigravity-agentic-skills vitest_runnerTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Vitest
Description
Modern JavaScript/TypeScript testing with Vitest including mocking and coverage.
When to Use
- Testing JavaScript/TypeScript
- React component testing
- Unit and integration tests
Core Patterns
Basic Tests
typescript
import { describe, it, expect } from 'vitest';
describe('math', () => {
it('should add numbers', () => {
expect(1 + 1).toBe(2);
});
it('should throw on invalid input', () => {
expect(() => divide(1, 0)).toThrow('Division by zero');
});
});Mocking
typescript
import { vi, describe, it, expect } from 'vitest';
// Mock module
vi.mock('./api', () => ({
fetchUser: vi.fn().mockResolvedValue({ id: 1 })
}));
// Mock function
const callback = vi.fn();
callback('arg');
expect(callback).toHaveBeenCalledWith('arg');Async Tests
typescript
it('should fetch data', async () => {
const data = await fetchData();
expect(data).toEqual({ id: 1 });
});
it('should reject on error', async () => {
await expect(fetchData()).rejects.toThrow('Error');
});React Testing
typescript
import { render, screen } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
it('should handle click', async () => {
const onClick = vi.fn();
render(<Button onClick={onClick}>Click</Button>);
await userEvent.click(screen.getByRole('button'));
expect(onClick).toHaveBeenCalled();
});🔄 Workflow
Aşama 1: Environment & Setup
- Vite Integration: dosyasının Vite ayarlarıyla senkronize olduğunu doğrula.
vitest.config.ts - Environment Choice: Web projeleri için veya
jsdom, backend içinhappy-domenvironment'ı seç.node - Global Mocks: Sık kullanılan harici servisler (API, LocalStorage) için içinde global mock'ları tanımla.
setup.ts
Aşama 2: Unit & Component Testing
- Isolation Layer: Bağımlılıkları ile izole ederek sadece hedef üniteyi test et.
vi.mock() - Assertion Strategy: metodlarını kullanarak beklenen sonuçları (be.truthy, toEqual, toBeCalled) doğrula.
expect - Snapshot Testing: UI bileşenlerindeki (Component) beklenmedik arayüz değişikliklerini ile yakala.
toMatchSnapshot()
Aşama 3: Performance & Coverage
- Watch Mode: Geliştirme sürecinde testleri modunda tutarak anlık geri bildirim al.
watch - Coverage Analysis: veya
v8provider kullanarak test kapsamını raporla.istanbul - Dependency Cleanup: ile testler arası veri kirliliğini (Pollution) önle.
vi.clearAllMocks()
Kontrol Noktaları
| Aşama | Doğrulama |
|---|---|
| 1 | Test dosyaları |
| 2 | Asenkron kodlar ( |
| 3 | Karmaşık nesne karşılaştırmalarında |
Vitest Runner v1.5 - With Workflow