Skills2026年4月11日·1 分钟阅读

Vitest — Next Generation Testing Framework Powered by Vite

Vitest is a blazing-fast unit testing framework powered by Vite, with native ESM, TypeScript, and JSX support. Jest-compatible API, instant HMR for tests, and in-source testing make it the go-to test runner for Vite projects.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install 267275ed-355f-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Vitest provides Jest-compatible testing with native Vite integration, ESM support, TypeScript, and sub-second watch mode.
§01

What it is

Vitest is a unit testing framework powered by Vite. It provides a Jest-compatible API with native ESM and TypeScript support, instant watch mode using Vite's dev server, snapshot testing, mocking, code coverage, and concurrent test execution. Vitest shares Vite's config, plugins, and transform pipeline, so tests run with the same settings as your application.

Vitest is for frontend and Node.js developers who use Vite as their build tool. If you are building with Vue, React, Svelte, or any Vite-based project, Vitest provides the fastest test experience with zero additional configuration.

§02

How it saves time or tokens

Vitest reuses Vite's transform pipeline, eliminating the need for separate Babel or TypeScript compilation in tests. Watch mode re-runs only affected tests in milliseconds. The Jest-compatible API means existing Jest tests often work with minimal changes -- just swap the import. For AI workflows, Vitest's simple config (reuse vite.config.ts) means less setup boilerplate and fewer tokens spent on test infrastructure.

§03

How to use

  1. Install Vitest: npm install -D vitest.
  2. Add a test script to package.json: "test": "vitest".
  3. Write test files with .test.ts or .spec.ts suffix and run with npm test.
§04

Example

// sum.ts
export function sum(a: number, b: number): number {
  return a + b;
}

// sum.test.ts
import { describe, it, expect } from 'vitest';
import { sum } from './sum';

describe('sum', () => {
  it('adds two numbers', () => {
    expect(sum(1, 2)).toBe(3);
  });

  it('handles negative numbers', () => {
    expect(sum(-1, 1)).toBe(0);
  });

  it('handles zero', () => {
    expect(sum(0, 0)).toBe(0);
  });
});
§05

Related on TokRepo

§06

Common pitfalls

  • Importing from 'jest' instead of 'vitest'. While the API is compatible, the imports must come from 'vitest'. Update your import statements when migrating from Jest.
  • Not configuring the test environment for DOM testing. By default Vitest runs in Node.js. Add environment: 'jsdom' or environment: 'happy-dom' in vitest.config.ts for component testing.
  • Forgetting that Vitest runs tests concurrently by default. Tests that share global state or modify the filesystem may need --sequence flag or describe.sequential() to avoid race conditions.

常见问题

Can I migrate from Jest to Vitest?+

Yes. Vitest is API-compatible with Jest for most use cases. Change your imports from jest to vitest, update the config to vitest.config.ts (or reuse vite.config.ts), and most tests run without changes. Complex Jest transforms may need adjustments.

Does Vitest support code coverage?+

Yes. Vitest supports code coverage via v8 (built-in) or istanbul. Run vitest --coverage to generate coverage reports. Install @vitest/coverage-v8 or @vitest/coverage-istanbul for the provider you prefer.

How fast is Vitest compared to Jest?+

Vitest is significantly faster for Vite projects because it reuses the Vite transform pipeline and avoids redundant compilation. Watch mode re-runs only affected tests in milliseconds. Cold start times are also faster due to native ESM support.

Does Vitest support React component testing?+

Yes. Use @testing-library/react with Vitest for component testing. Set the environment to jsdom or happy-dom in your config and install the testing library. The API works identically to Jest-based React testing.

Can I use Vitest without Vite?+

Technically yes, but Vitest is designed to share Vite's config and plugin pipeline. Without Vite, you lose the main performance advantages. For non-Vite projects, Jest or Node.js built-in test runner may be more appropriate.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产