Token导航 LogoToken导航TokenDH.com
开发敏感数据github未标认证来源可访问许可证需确认审计通过

vitestVitest 测试

Agent Skill

用于辅助 API 设计、接口文档、请求响应结构和服务集成说明。它适合让 Agent 梳理 endpoint、生成 OpenAPI 草稿、检查字段命名、整理错误码或辅助前后端联调。使用时需要确认真实业务语义、鉴权方式、分页和错误处理规则;涉及生成接口文档时,应避免凭空补字段,最好从现有代码、schema 或接口样例中提取事实。

总安装

242

周安装

10

GitHub Stars

6

下载量

79
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:vitest(Vitest 测试)
来源仓库:https://github.com/ghosttypes/ff-5mp-api-ts
仓库路径:skills/vitest
安装命令:
npx skills add https://github.com/ghosttypes/ff-5mp-api-ts --skill vitest
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/ghosttypes/ff-5mp-api-ts --skill vitest

简介

Vitest 测试技能辅助 API 设计和接口文档生成,支持字段命名和结构检查。

  • 适用于服务集成说明和联调支持等前后端协作场景。
  • 通过 GitHub 仓库安装,使用 npx skills add 命令启用技能模块。
  • 使用时需结合现有 schema 或接口样例,避免生成与实际不符的接口定义。
  • vitest 属于开发类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Vitest

Overview

Vitest is a blazing-fast unit test framework powered by Vite. This skill provides complete Vitest documentation, APIs, configuration options, and migration guides to enable flawless test development in any JavaScript or TypeScript project.

Key capabilities:

  • Vite-native for instant feedback and HMR
  • Jest-compatible API for easy migration
  • Works in Vite and non-Vite projects
  • Built-in TypeScript, JSX, and ESM support
  • Native code coverage (V8 and Istanbul)
  • Browser mode for component testing
  • Workspace support for monorepos

Quick Start Decision Tree

Is this a new project or adding tests to an existing project?

  • New project: Start with "Installation & Setup"
  • Existing project: Check framework type below

What type of project is it?

  • Vite/Vue/React/Svelte: Use Vite integration (see "Vite Project Setup")
  • Non-Vite (Next.js, Angular, vanilla): Use standalone mode (see "Standalone Project Setup")
  • Monorepo: Use workspace configuration (see "Workspace Setup")

Are you migrating from Jest?

  • Yes: See "Migration from Jest" section

Installation & Setup

Basic Installation

# Install Vitest
npm install -D vitest

# Add test script to package.json
{
  "scripts": {
    "test": "vitest",
    "test:ui": "vitest --ui"
  }
}

Vite Project Setup

For projects already using Vite, extend the existing vite.config.ts:

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    // test options
  },
  // your existing Vite config
})

Vitest automatically shares your Vite config, plugins, and transformations.

Standalone Project Setup

For non-Vite projects, create vitest.config.ts:

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    include: ['**/*.{test,spec}.{js,ts}'],
    environment: 'node' // or 'jsdom' for browser environment
  }
})

Framework-specific setup guides:

  • Next.js: See references/guide/index.md for configuration
  • Angular: Use environment: 'jsdom' with appropriate transformers
  • React/Vue/Svelte: Extend Vite config for best experience

TypeScript Setup

Vitest supports TypeScript out of the box. Ensure tsconfig.json includes test files:

{
  "include": ["src/**/*", "**/*.test.ts"]
}

Writing Tests

Basic Test Structure

// math.test.ts
import { describe, it, expect } from 'vitest'
import { add } from './math'

describe('add', () => {
  it('should add two numbers', () => {
    expect(add(1, 2)).toBe(3)
  })

  it('should handle negative numbers', () => {
    expect(add(-1, -2)).toBe(-3)
  })
})

Test Organization

  • Use describe blocks to group related tests
  • Name test files with .test.ts or .spec.ts suffix
  • Co-locate tests with source code or in __tests__ directories
  • Use test context for sharing data between tests (see references/guide/test-context.md)

Lifecycle Hooks

import { beforeAll, beforeEach, afterAll, afterEach } from 'vitest'

describe('database tests', () => {
  beforeAll(async () => {
    // Runs once before all tests
    await connectDatabase()
  })

  beforeEach(async () => {
    // Runs before each test
    await clearDatabase()
  })

  afterEach(async () => {
    // Runs after each test
    await cleanup()
  })

  afterAll(async () => {
    // Runs once after all tests
    await disconnectDatabase()
  })
})

For complete lifecycle reference, see references/api/hooks.md.

Running Tests

# Run all tests once
vitest run

# Watch mode (default)
vitest

# Run matching pattern
vitest --testNamePattern="should add"

# UI mode
vitest --ui

# Coverage
vitest --coverage

See references/guide/cli.md for complete CLI reference.

Mocking

Vitest provides comprehensive mocking capabilities through the vi utility.

Function Mocking

import { vi, describe, it, expect } from 'vitest'

const mockFn = vi.fn()
mockFn('hello')
expect(mockFn).toHaveBeenCalledWith('hello')

// With return value
const mocked = vi.fn().mockReturnValue('test')
expect(mocked()).toBe('test')

// With implementation
const callback = vi.fn((x) => x + 1)
expect(callback(1)).toBe(2)

Module Mocking

import { vi, expect, it } from 'vitest'
import { fetchData } from './api'

// Mock entire module
vi.mock('./api', () => ({
  fetchData: vi.fn(() => Promise.resolve('mocked data'))
}))

it('uses mocked API', async () => {
  const data = await fetchData()
  expect(data).toBe('mocked data')
})

Timer Mocking

import { vi, beforeEach, expect, it } from 'vitest'

beforeEach(() => {
  vi.useFakeTimers()
})

it('calls callback after timeout', () => {
  const callback = vi.fn()
  setTimeout(callback, 1000)

  vi.advanceTimersByTime(1000)
  expect(callback).toHaveBeenCalled()
})

Complete mocking reference:

  • Functions: references/guide/mocking/functions.md
  • Modules: references/guide/mocking/modules.md
  • Timers: references/guide/mocking/timers.md
  • Globals: references/guide/mocking/globals.md
  • Dates: references/guide/mocking/dates.md

Configuration Patterns

Common Configuration Scenarios

1. Browser Environment (React/Vue/Svelte)

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    environment: 'jsdom',
    globals: true,
    setupFiles: ['./src/test/setup.ts']
  }
})

2. Code Coverage

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8', // or 'istanbul'
      include: ['src/**/*.{js,ts}'],
      exclude: ['src/**/*.test.{js,ts}', 'src/**/*.config.{js,ts}']
    }
  }
})

3. Monorepo/Workspace

export default defineConfig({
  test: {
    workspace: [
      'packages/*',
      'apps/*'
    ]
  }
})

4. Browser Mode (Component Testing)

export default defineConfig({
  test: {
    browser: {
      enabled: true,
      provider: 'playwright', // or 'webdriverio'
      headless: true
    }
  }
})

See references/config/INDEX.md for complete configuration reference.

Advanced Features

Snapshot Testing

import { expect, it } from 'vitest'

it('matches snapshot', () => {
  const data = { foo: 'bar' }
  expect(data).toMatchSnapshot()
})

See references/guide/snapshot.md for complete snapshot guide.

In-Source Testing

Write tests directly next to code:

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

// @ts-ignore
if (import.meta.vitest) {
  const { it, expect } = import.meta.vitest
  it('adds numbers', () => {
    expect(add(1, 2)).toBe(3)
  })
}

See references/guide/in-source.md for configuration.

Test Tags

Organize and filter tests by tags:

import { test } from 'vitest'

test('slow integration test', { tags: ['@slow', '@integration'] }, () => {
  // test code
})

// Run only fast tests
// vitest --tags @fast

See references/guide/test-tags.md for usage.

Browser Testing

Test components in real browser:

import { expect, test } from 'vitest'
import { render } from '@testing-library/vue'

test('renders button', async () => {
  const { getByText } = render(Button, {
    props: { label: 'Click me' }
  })

  expect(getByText('Click me')).toBeTruthy()
})

See references/guide/browser/ for complete browser testing guide.

Migration

From Jest to Vitest

Vitest is largely compatible with Jest. The migration process:

  1. Install Vitest: npm install -D vitest
  2. Update configuration: Replace jest.config.js with vitest.config.ts
  3. Update scripts: Change test script to use vitest
  4. Update imports: Replace @jest/globals with vitest
  5. Verify mocks: Most Jest mocks work unchanged

Key differences:

  • Auto-mocked modules: Vitest doesn't auto-mock by default
  • Timer mocks: Use vi.useFakeTimers() instead of jest.useFakeTimers()
  • Environment variables: Use process.env directly

See references/guide/comparisons.md for detailed Jest comparison.

From Older Vitest Versions

Migrating to Vitest 4.0:

  • Coverage provider changes: V8 now uses AST-based remapping
  • Removed coverage.all and coverage.extensions - use coverage.include instead
  • Coverage ignore hints updated - see references/guide/migration.md

See migration guides:

  • Vitest 4.0: references/guide/migration.md#vitest-4
  • Vitest 3.0: references/vitest-3.md
  • Vitest 3.2: references/vitest-3-2.md

Troubleshooting

Common Issues

Tests run in wrong environment:

// vitest.config.ts
export default defineConfig({
  test: {
    environment: 'jsdom' // for browser tests
    // or 'node' for server tests
  }
})

Modules not transforming:

  • Check transformMode configuration
  • Ensure dependencies are in deps.interopDefault

Timeouts:

  • Increase timeout: test({timeout: 10000}, () => {...})
  • Or globally: test: {timeout: 10000}

Watch mode not detecting changes:

  • Check file inclusion patterns
  • Verify include and exclude in config

See references/guide/common-errors.md for more troubleshooting.

Resources

This skill includes comprehensive Vitest documentation organized for progressive disclosure:

Documentation Indices

Key Documentation

Getting Started:

Core Concepts:

Mocking:

API Reference:

Configuration:

Migration:

Advanced:

When working with Vitest, consult the appropriate reference file based on your task. Start with the guide for conceptual understanding, then refer to API/config references for specific implementation details.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude

32.51%
按下载量换算26

Codex

32.21%
按下载量换算25

Cursor

20.16%
按下载量换算16

Gemini CLI

10.19%
按下载量换算8

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

敏感数据

该 Skill 可能接触密钥、Token、环境变量或敏感配置,应进入高风险复核队列,默认不自动发布。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills