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 測試執行器快照的完整文件,請參閱文件 > 測試執行器 > 快照。