# @vitest/snapshot 中文文档

规范地址：https://npmcn.com/package/%40vitest/snapshot/

版本：5\.1\.0
npm 元数据更新时间：2026-11-02T16:12:13.613Z
中文译文更新时间：2026-11-05T15:09:55.397Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40vitest/snapshot
原文对照：https://npmcn.com/package/%40vitest/snapshot/?lang=original
项目主页：https://vitest.dev/guide/snapshot
源码仓库：https://github.com/vitest-dev/vitest

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：65.1 kB，13 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：4 个
- 首次发布：2023-04-09T13:39:24.245Z
- 最新版本发布：2026-11-02T16:12:13.288Z
- 近 30 天下载量：438251590（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：195（近 12 个月 51 个）

## 中文简介

虚拟快照管理器

## 安装

```sh
npm install @vitest/snapshot
```

也可以使用：`pnpm add @vitest/snapshot`、`yarn add @vitest/snapshot`、`bun add @vitest/snapshot`

## README

# @ vitest/ snapshot (英语)

[![NPM version](https://img.shields.io/npm/v/@vitest/snapshot?color=a1b858&label=)](https://npmx.dev/package/@vitest/snapshot)

轻量级实施杰斯快照.

## 使用量

```js
import { SnapshotClient } from '@vitest/snapshot'
import { NodeSnapshotEnvironment } from '@vitest/snapshot/environment'
import { SnapshotManager } from '@vitest/snapshot/manager'

const client = new SnapshotClient({
  // you need to provide your own equality check implementation if you use it
  // this function is called when `.toMatchSnapshot({ property: 1 })` is called
  isEqual: (received, expected) =>
    equals(received, expected, [iterableEquality, subsetEquality]),
})

// class that implements snapshot saving and reading
// by default uses fs module, but you can provide your own implementation depending on the environment
const environment = new NodeSnapshotEnvironment()

// you need to implement this yourselves,
// this depends on your runner
function getCurrentFilepath() {
  return '/file.spec.js'
}
function getCurrentTestName() {
  return 'test1'
}

// example for inline snapshots, nothing is required to support regular snapshots,
// just call `assert` with `isInline: false`
function wrapper(received) {
  function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
    client.assert({
      received,
      message,
      isInline: true,
      inlineSnapshot,
      filepath: getCurrentFilepath(),
      name: getCurrentTestName(),
    })
  }
  return {
    // the name is hard-coded, it should be inside another function, so Vitest can find the actual test file where it was called (parses call stack trace + 2)
    // you can override this behaviour in SnapshotState's `_inferInlineSnapshotStack` method by providing your own SnapshotState to SnapshotClient constructor
    toMatchInlineSnapshot: (...args) => __INLINE_SNAPSHOT__(...args),
  }
}

const options = {
  updateSnapshot: 'new',
  snapshotEnvironment: environment,
}

await client.startCurrentRun(
  getCurrentFilepath(),
  getCurrentTestName(),
  options
)

// this will save snapshot to a file which is returned by "snapshotEnvironment.resolvePath"
client.assert({
  received: 'some text',
  isInline: false,
})

// uses "pretty-format", so it requires quotes
// also naming is hard-coded when parsing test files
wrapper('text 1').toMatchInlineSnapshot()
wrapper('text 2').toMatchInlineSnapshot('"text 2"')

const result = await client.finishCurrentRun() // this saves files and returns SnapshotResult

// you can use manager to manage several clients
const manager = new SnapshotManager(options)
manager.add(result)

// do something
// and then read the summary

console.log(manager.summary)
```

[GitHub 图像](https://github.com/vitest-dev/vitest/tree/main/packages/snapshot) | [文档](https://vitest.dev/guide/snapshot)

## 常见问题

### @vitest/snapshot 是什么？

虚拟快照管理器

### 如何安装 @vitest/snapshot？

在项目目录运行 npm install @vitest/snapshot，或使用 pnpm add / yarn add / bun add。

### @vitest/snapshot 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @vitest/snapshot 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @vitest/snapshot 可以商用吗？

@vitest/snapshot 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @vitest/snapshot 的下载量有多少？

根据 npm 官方下载统计，@vitest/snapshot 在 2026/10/07 – 2026/11/05 这 30 天内被下载 438,251,590 次，日均约 14,608,386 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @vitest/snapshot 还在维护吗？

最新版本 5\.1\.0 发布于 2026/11/03（5 天前）；至今共发布 195 个版本，其中过去 12 个月发布了 51 个；首次发布于 2023/04/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

