> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 `bun test` 中更新快照

Bun 的测试运行器通过 `.toMatchSnapshot()` 支持类似 Jest 的快照测试。

```ts snap.test.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
import { test, expect } from "bun:test";

test("snapshot", () => {
  expect({ foo: "bar" }).toMatchSnapshot();
});
```

***

第一次执行此测试时，Bun 会将快照文件写入磁盘，存放在与测试文件同级的名为 `__snapshots__` 的目录中。

```txt File Tree icon="folder-tree" theme={"theme":{"light":"github-light","dark":"dracula"}}
test
├── __snapshots__
│   └── snap.test.ts.snap
└── snap.test.ts
```

***

要重新生成快照，请使用 `--update-snapshots` 标志。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun test --update-snapshots
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
test/snap.test.ts:
✓ snapshot [0.86ms]

 1 pass
 0 fail
 snapshots: +1 added # 快照已重新生成
 1 expect() calls
Ran 1 tests across 1 files. [102.00ms]
```

***

完整的 Bun 测试运行器快照文档请参见 [Docs > Test Runner > Snapshots](/test/snapshots)。
