Bun 的测试运行器通过 .toMatchSnapshot() 支持 Jest 风格的快照测试。
ts
import { test, expect } from "bun:test";
test("快照", () => {
expect({ foo: "bar" }).toMatchSnapshot();
});第一次执行此测试时,Bun 会将快照文件写入名为 __snapshots__ 的目录中的磁盘上,该目录与测试文件放在一起。
txt
test
├── __snapshots__
│ └── snap.test.ts.snap
└── snap.test.ts要重新生成快照,请使用 --update-snapshots 标志。
sh
bun test --update-snapshotstxt
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 测试运行器进行快照的完整文档。