@vitest/snapshot

中文文档
5.1.0发布于 2026/11/03MITTypeScriptES Module构建来源已验证近 30 天下载 4.4亿

虚拟快照管理器

中文使用文档

@ vitest/ snapshot (英语)

NPM version

轻量级实施杰斯快照.

使用量

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 图像 | 文档

版本与发布记录

@vitest/snapshot 共发布 195 个版本,首次发布于 2023/04/09;过去 12 个月发布了 51 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.1.0latest2026/11/035 天前
5.1.0-beta.1beta2026/10/2019 天前
4.1.12V42026/10/1523 天前
5.0.52026/10/1523 天前
5.0.42026/10/1226 天前
5.0.32026/09/301 个月前

其他发布通道:V3 → 3.2.7,rc → 5.0.0-rc.4,V4 → 4.1.12,beta → 5.1.0-beta.1。

关于 @vitest/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 和提交记录判断。