testing-vitest
Set up and write Vitest unit/component tests for Next.js / React projects. Detection-first: adapts to whatever test runner is already installed. Used by…
от nghiahsgs★ 31 звезда на GitHub
Установить testing-vitest в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
testing-vitest — Vitest Setup + Patterns
Step 0 — Detect existing test runner
Read package.json → scan devDependencies:
| Found | Action |
|---|---|
vitest |
Use existing config. Skip install. Read vitest.config.* to understand setup. |
jest |
Project is Jest-based. Offer to write Jest-style tests OR migrate to Vitest (warn: migration is a separate branch, out of scope here). Default = write Jest style. |
| neither | New setup. Continue to Step 1. |
NEVER install Vitest if Jest is already present — user must explicitly request migration.
Step 1 — Install (new setup only)
npm install -D vitest @vitest/ui @testing-library/react @testing-library/jest-dom @testing-library/user-event jsdom @vitejs/plugin-react
Add scripts to package.json:
"test": "vitest run",
"test:watch": "vitest",
"test:ui": "vitest --ui",
"test:coverage": "vitest run --coverage"
Step 2 — Config file
Create vitest.config.ts at repo root:
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import path from 'node:path'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
globals: true,
setupFiles: ['./test/setup.ts'],
css: true,
coverage: { provider: 'v8', reporter: ['text', 'html'] },
},
resolve: {
alias: { '@': path.resolve(__dirname, './') },
},
})
Create test/setup.ts:
import '@testing-library/jest-dom/vitest'
import { afterEach } from 'vitest'
import { cleanup } from '@testing-library/react'
afterEach(() => cleanup())
Add to tsconfig.json types: "types": ["vitest/globals", "@testing-library/jest-dom"]
Step 3 — Patterns
Pattern: pure function (happy + edge)
// lib/cart.test.ts
import { describe, it, expect } from 'vitest'
import { calcTotal, applyDiscount } from './cart'
describe('calcTotal', () => {
it('sums item prices', () => {
expect(calcTotal([{ price: 100 }, { price: 50 }])).toBe(150)
})
it('handles empty cart', () => {
expect(calcTotal([])).toBe(0)
})
it('ignores negative prices', () => {
expect(calcTotal([{ price: -10 }, { price: 5 }])).toBe(5)
})
})
Pattern: React component
// components/LoginForm.test.tsx
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, it, expect, vi } from 'vitest'
import { LoginForm } from './LoginForm'
describe('LoginForm', () => {
it('submits with entered email', async () => {
const onSubmit = vi.fn()
render(<LoginForm onSubmit={onSubmit} />)
await userEvent.type(screen.getByLabelText(/email/i), '[email protected]')
await userEvent.click(screen.getByRole('button', { name: /login/i }))
expect(onSubmit).toHaveBeenCalledWith({ email: '[email protected]' })
})
})
Pattern: mock Supabase client
import { vi } from 'vitest'
vi.mock('@/lib/supabase', () => ({
supabase: {
from: vi.fn(() => ({
select: vi.fn(() => ({ eq: vi.fn(() => ({ data: [{ id: 1 }], error: null })) })),
})),
auth: { getUser: vi.fn(() => ({ data: { user: { id: 'u1' } }, error: null })) },
},
}))
Pattern: Next.js Server Component
Server Components can't mount via RTL directly — test the async function's return shape:
import { describe, it, expect } from 'vitest'
import Page from './page'
it('returns items from DB', async () => {
const result = await Page()
expect(result.props.children).toBeDefined()
})
For full render, prefer Playwright (E2E).
Pattern: API route handler
// app/api/hello/route.test.ts
import { GET } from './route'
import { NextRequest } from 'next/server'
it('returns 200 with message', async () => {
const req = new NextRequest('http://localhost/api/hello')
const res = await GET(req)
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ message: 'hello' })
})
Step 4 — Gotchas
- "ReferenceError: window is not defined" → test file imports something needing jsdom, but
environmentnot set. Fix: add// @vitest-environment jsdomat top. - Module alias
@/not found → checkresolve.aliasinvitest.config.tsmatchestsconfig.jsonpaths. - Next.js server-only modules (
next/headers,cookies()) → mock them; real ones throw outside request context. - CSS imports fail → keep
css: truein vitest config OR usevite-plugin-virtualto stub them.
Constraints
- Adapt to existing test runner — do NOT install vitest alongside jest
- Test files sit next to source (
lib/x.ts→lib/x.test.ts) for visibility - Never commit
.vitest/cache directory — add to.gitignoreif missing
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
- 01
Read - 02
Write - 03
Edit - 04
Bash - 05
Grep
FAQ
Что делает скилл testing-vitest?
Set up and write Vitest unit/component tests for Next.js / React projects. Detection-first: adapts to whatever test runner is already installed. Used by branches/maintain/test.md when gen'ing unit or component tests. Trigger phrases (EN + VN): "vitest setup", "unit test", "gen test", "viết test", "test component", "jsdom test", "@testing-library".
Как установить скилл testing-vitest?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл testing-vitest запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
AnthropicGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
academy-guide
AnthropicStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
internal-comms
AnthropicA set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
skill-development
AnthropicThis skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…