Bun 的测试运行器通过 .toMatchSnapshot() 支持 Jest 风格的快照测试。
import { test, expect } from "bun:test";
test("snapshot", () => {
  expect({ foo: "bar" }).toMatchSnapshot();
});
首次执行此测试时,Bun 会在测试文件旁边的 __snapshots__ 目录中向磁盘写入一个快照文件。
test
├── __snapshots__
│   └── snap.test.ts.snap
└── snap.test.ts
要重新生成快照,请使用 --update-snapshots 标志。
bun test --update-snapshotsbun test v1.3.0 (9c68abdb)
test/snap.test.ts:
✓ snapshot [0.86ms]
 1 pass
 0 fail
 snapshots: +1 added # the snapshot was regenerated
 1 expect() calls
Ran 1 tests across 1 files. [102.00ms]有关 Bun 测试运行器快照的完整文档,请参阅文档 > 测试运行器 > 快照。