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 by www.bunjs.com.cn 編集