Skip to main content
Bun 的测试运行器通过 .toMatchSnapshot() 支持类似 Jest 的快照测试。
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79snap.test.ts
import { test, expect } from "bun:test";

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

第一次执行此测试时,Bun 会将快照文件写入磁盘,存放在与测试文件同级的名为 __snapshots__ 的目录中。
File Tree
test
├── __snapshots__
│   └── snap.test.ts.snap
└── snap.test.ts

要重新生成快照,请使用 --update-snapshots 标志。
terminal
bun test --update-snapshots
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