Bun 的測試執行器支援透過 .toMatchSnapshot()
進行 Jest 風格的快照測試。
.toMatchInlineSnapshot()
方法尚未支援。
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-snapshots
bun test v1.x (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 測試執行器進行模擬的完整文件。