vitest

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Vitest

Vitest

Vite-native testing framework with Jest-compatible API.
一款与Jest API兼容的Vite原生测试框架。

When to Use

使用场景

  • Writing unit/integration tests for Vite projects
  • Testing Vue/React/Svelte components
  • Mocking modules, timers, or dates
  • Running concurrent/parallel tests
  • Type testing with TypeScript
  • 为Vite项目编写单元/集成测试
  • 测试Vue/React/Svelte组件
  • 模拟模块、计时器或日期
  • 运行并发/并行测试
  • 使用TypeScript进行类型测试

Quick Start

快速开始

bash
npm i -D vitest
ts
// vitest.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
    environment: 'node',  // or 'jsdom' for DOM tests
  },
})
ts
// example.test.ts
import { describe, expect, it, vi } from 'vitest'

describe('math', () => {
  it('adds numbers', () => {
    expect(1 + 1).toBe(2)
  })
})
bash
npm i -D vitest
ts
// vitest.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
    environment: 'node',  // or 'jsdom' for DOM tests
  },
})
ts
// example.test.ts
import { describe, expect, it, vi } from 'vitest'

describe('math', () => {
  it('adds numbers', () => {
    expect(1 + 1).toBe(2)
  })
})

Reference Files

参考文件

TaskFile
Configuration, CLI, projectsconfig.md
test/describe, hooks, fixturestest-api.md
vi.fn, vi.mock, timers, spiesmocking.md
expect, snapshots, coverage, filteringutilities.md
Environments, type testing, browser modeadvanced.md
任务文件
配置、CLI、项目config.md
test/describe、钩子、测试夹具test-api.md
vi.fn、vi.mock、计时器、间谍mocking.md
expect、快照、覆盖率、过滤utilities.md
测试环境、类型测试、浏览器模式advanced.md

Loading Files

文件加载建议

Consider loading these reference files based on your task:
  • references/config.md - if setting up vitest.config.ts, CLI, or workspace projects
  • references/test-api.md - if writing test/describe blocks, using hooks, or test fixtures
  • references/mocking.md - if mocking modules, timers, dates, or using spies
  • references/utilities.md - if writing assertions, snapshots, or configuring coverage
  • references/advanced.md - if configuring test environments, type testing, or browser mode
DO NOT load all files at once. Load only what's relevant to your current task.
请根据当前任务加载对应的参考文件:
  • references/config.md - 若需设置vitest.config.ts、CLI或工作区项目
  • references/test-api.md - 若需编写test/describe代码块、使用钩子或测试夹具
  • references/mocking.md - 若需模拟模块、计时器、日期或使用间谍
  • references/utilities.md - 若需编写断言、快照或配置覆盖率
  • references/advanced.md - 若需配置测试环境、类型测试或浏览器模式
请勿一次性加载所有文件。仅加载与当前任务相关的文件。

Cross-Skill References

跨技能参考

  • Vue component testing → Use
    vue
    skill for component patterns
  • Library testing → Use
    ts-library
    skill for library patterns
  • Vite configuration → Use
    vite
    skill for shared config
  • Vue组件测试 → 使用
    vue
    技能获取组件模式
  • 库测试 → 使用
    ts-library
    技能获取库模式
  • Vite配置 → 使用
    vite
    技能获取共享配置