Przejdź do treści
Biblioteka skilli
Programowanie · Testowanie

vitest

/vitest

Pomaga pisać szybkie testy jednostkowe i integracyjne w Vitest dla projektów Vite, Vue, React i TypeScript: mocking, snapshoty i wsparcie ESM.

Repozytorium
Agent-Skills ↗
Licencja
MIT
Ostatnia zmiana repozytorium
01.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
1
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
1
Przestarzałość

Brak sygnałów. Repozytorium zmienione 01.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Pomaga pisać szybkie testy jednostkowe i integracyjne w Vitest dla projektów Vite, Vue, React i TypeScript: mocking, snapshoty i wsparcie ESM.

Oryginalny opis (EN)

Expert Vitest testing assistance covering Vite-native test execution, mocking, snapshots, and ESM support. Use when writing fast unit and integration tests for Vite, Vue, React, or TypeScript projects.

Jak działa

  1. When to Use
  2. Quick Start
  3. Core Concepts
  4. Common Patterns
  5. Best Practices
  6. Troubleshooting
  7. References
Sekcje z nagłówków SKILL.md, w oryginale · 150 linii, 617 słów
Podgląd SKILL.md
---
name: vitest
description: Expert Vitest testing assistance covering Vite-native test execution, mocking, snapshots, and ESM support. Use when writing fast unit and integration tests for Vite, Vue, React, or TypeScript projects.
---

# Vitest

Vitest is a next-generation testing framework powered by Vite. It uses the same configuration (vite.config.ts), transforms, and plugins as your app, making it incredibly fast and creating a unified dev/test environment.

## When to Use

- **Modern Vite & ESM Unit Testing**: The lightning-fast, native test runner for Vite-powered React, Vue, Svelte, Solid, and Node.js projects.
- **Drop-in Jest Replacement**: Migrating from Jest to Vitest with identical `describe`, `it`, `expect`, and `vi` APIs with zero config overhead.
- **Instant Watch Mode with HMR**: Re-running only affected tests instantaneously when source files change.
- **Native TypeScript & JSX Support**: Running TypeScript and JSX out of the box without separate transpilation pipelines (ts-jest/babel).

## Quick Start

```typescript
// vite.config.ts
/// <reference types="vitest" />
import { defineConfig } from "vite";

export default defineConfig({
  test: {
    globals: true, // enables 'describe', 'test', 'expect' globally
    environment: "jsdom",
  },
});

// basic.test.ts
import { expect, test } from "vitest";

test("math", () => {
  expect(1 + 1).toBe(2);
});
```

## Core Concepts

### Unified Vite Configuration (vite.config.ts)

Shares identical plugins, alias paths, and CSS pipelines between development and testing:

```typescript
// vitest.config.ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import path from "path";

export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    environment: "happy-dom",
    setupFiles: "./tests/setup.ts",
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
    coverage: {
      provider: "v8",
      reporter: ["text", "json", "html"],
    },
  },
});
```

### Modern Mocking Utilities (`vi.fn()`, `vi.mock()`)

High-performance module and timer mocking engine:

```typescript
import { vi, test, expect } from "vitest";
import { fetchAnalytics } from "./analytics";

vi.mock("./analytics", () => ({
  fetchAnalytics: vi.fn().mockResolvedValue({ pageViews: 1420 }),
}));

test("loads analytics data cleanly", async () => {
  const data = await fetchAnalytics();
  expect(data.pageViews).toBe(1420);
  expect(fetchAnalytics).toHaveBeenCalledOnce();
});
```

### In-Source Testing

Write tests directly alongside source code (stripped automatically in production builds):

```typescript
// src/math.ts
export function add(a: number, b: number): number {
  return a + b;
}

if (import.meta.vitest) {
  const { it, expect } = import.meta.vitest;
  it("adds two numbers correctly", () => {
    expect(add(2, 3)).toBe(5);
  });
}
```

## Common Patterns

### In-Source Testing and Fast Mocking

**Problem**: Context switching between source files and test files for small pure utility functions.

**Solution**:
Use Vitest in-source testing or vi mocks:

```typescript
import { vi, test, expect } from "vitest";
import { sendNotification } from "./notifier";

vi.mock("./notifier", () => ({
  sendNotification: vi.fn(),
}));

test("triggers notification with formatted message", async () => {
  sendNotification("Welcome to Vitest");
  expect(sendNotification).toHaveBeenCalledWith("Welcome to Vitest");
});
```

## Best Practices

**Do**:

- Use `happy-dom` Instead of `jsdom`: Choose `environment: 'happy-dom'` for 2-3x faster DOM simulation performance.
- Leverage V8 Coverage Provider: Use `coverage.provider: 'v8'` for instant, precise native code coverage reports.
- Use `vi.hoisted()` for Dynamic Mock Pre-Execution: Initialize variables needed inside `vi.mock()` factories cleanly.
- Use In-Source Testing for Pure Utility Modules: Colocate tests inside utility files for rapid feedback during development.

**Don't**:

- Use separate Babel / ts-jest configurations: Rely on Vitest's native Vite pipeline; eliminate extra transpilers.
- Forget `vi.restoreAllMocks()`: Reset spies between tests to prevent test pollution in watch mode.
- Mock global fetch manually: Use `vi.spyOn(globalThis, 'fetch')` or MSW for standardized network mocks.

## Troubleshooting

| Error                                     | Cause                                                                   | Solution                                                                    |
| :---------------------------------------- | :---------------------------------------------------------------------- | :-------------------------------------------------------------------------- |
| `Error: Failed to resolve import`         | Path alias defined in `tsconfig.json` not mirrored in `vite.config.ts`. | Install `vite-tsconfig-paths` plugin in `vite.config.ts`.                   |
| `ReferenceError: document is not defined` | Testing DOM/browser components without happy-dom or jsdom.              | Set `// @vitest-environment jsdom` at top of file or in `vitest.config.ts`. |
| `TypeError: vi.mock is not hoisted`       | Using variables defined outside `vi.mock` inside the mock factory.      | Pass inline implementations or use `vi.hoisted` to declare variables.       |

## References

- [Vitest Documentation](https://vitest.dev/)

Wersje

RepozytoriumWynikLicencja
Ta strona · G1Joshi/Agent-Skills100MIT
TerminalSkills/skills100Apache-2.0
zenobi-us/dotfiles100MIT
itechmeat/llm-code100MIT
claude-dev-suite/claude-dev-suite100MIT
aAAaqwq/AGI-Super-Team100MIT
jezweb/claude-skills100MIT
el-feo/ai-context100MIT
hamsurang/kit100MIT

Migawka bazy z 28.10.2026 · wersja paczki 9ecb099d786d (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100vitest-testing
    Prowadzi testowanie TypeScript i JavaScript w Vitest: szybkie testy jednostkowe i integracyjne, natywne ESM, HMR z Vite i rozbudowany mocking.
  • 100vitest-testing
    Obsługuje Vitest, runner testów oparty na Vite: ESM, tryb watch i UI, pokrycie, mocking i snapshoty, także przy migracji z Jest.
  • 100vitest-skill
    Generuje testy Vitest w JavaScript i TypeScript z szybkością natywną dla Vite, API zgodnym z Jest i obsługą ESM.
  • 100vitest-skill
    Generuje testy Vitest w JavaScript i TypeScript z szybkością natywną dla Vite, API zgodnym z Jest i obsługą ESM.
  • 100typescript-testing
    Pisze testy TypeScript w Jest lub Vitest: testy jednostkowe i integracyjne, mocki oraz pokrycie kodu.
  • 100vitest-testing
    Stosuje wzorce i dobre praktyki Vitest: testy jednostkowe i integracyjne, vitest.config, mocking przez vi.mock i vi.fn, snapshoty i pokrycie.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.