Skip to content

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-snapshots
txt
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 測試運行器進行快照的完整文檔。

Bun學習網由www.bunjs.com.cn整理維護