Bun 의 테스트 러너는 .toMatchSnapshot() 을 통한 Jest 스타일의 스냅샷 테스트를 지원합니다.
ts
import { test, expect } from "bun:test";
test("스냅샷", () => {
expect({ foo: "bar" }).toMatchSnapshot();
});이 테스트가 처음 실행될 때 Bun 은 expect() 에 전달된 값을 평가하여 테스트 파일과 함께 있는 __snapshots__ 라는 디렉터리에 디스크에 씁니다. (출력에서 snapshots: +1 added 줄을 확인하세요.)
sh
bun test test/snaptxt
test/snap.test.ts:
✓ snapshot [1.48ms]
1 pass
0 fail
snapshots: +1 added
1 expect() calls
Ran 1 tests across 1 files. [82.00ms]__snapshots__ 디렉터리에는 디렉터리의 각 테스트 파일에 대한 .snap 파일이 포함되어 있습니다.
txt
test
├── __snapshots__
│ └── snap.test.ts.snap
└── snap.test.tssnap.test.ts.snap 파일은 expect() 에 전달된 값의 직렬화된 버전을 내보내는 JavaScript 파일입니다. {foo: "bar"} 객체는 JSON 으로 직렬화되었습니다.
js
// Bun Snapshot v1, https://bun.com/docs/test/snapshots
exports[`snapshot 1`] = `
{
"foo": "bar",
}
`;나중에 이 테스트 파일이 다시 실행될 때 Bun 은 스냅샷 파일을 읽고 expect() 에 전달된 값과 비교합니다. 값이 다르면 테스트가 실패합니다.
sh
bun testtxt
bun test v1.3.3 (9c68abdb)
test/snap.test.ts:
✓ snapshot [1.05ms]
1 pass
0 fail
1 snapshots, 1 expect() calls
Ran 1 tests across 1 files. [101.00ms]스냅샷을 업데이트하려면 --update-snapshots 플래그를 사용하세요.
sh
bun test --update-snapshotstxt
bun test v1.3.3 (9c68abdb)
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 테스트 러너로 스냅샷에 대한 전체 문서는 문서 > 테스트 러너 > 스냅샷 을 참조하세요.