快照测试保存值的输出并将其与未来的测试运行进行比较。这对于 UI 组件、复杂对象或任何需要保持一致的输出特别有用。
基本快照
快照测试使用 .toMatchSnapshot() 匹配器编写:
ts
import { test, expect } from "bun:test";
test("snap", () => {
expect("foo").toMatchSnapshot();
});第一次运行此测试时,expect 的参数将被序列化并写入测试文件旁边 __snapshots__ 目录中的特殊快照文件。
快照文件
运行上述测试后,Bun 将创建:
text
your-project/
├── snap.test.ts
└── __snapshots__/
└── snap.test.ts.snap快照文件包含:
ts
// Bun Snapshot v1, https://bun.com/docs/test/snapshots
exports[`snap 1`] = `"foo"`;在未来的运行中,参数将与磁盘上的快照进行比较。
更新快照
可以使用以下命令重新生成快照:
bash
bun test --update-snapshots这在以下情况下很有用:
- 你已故意更改了输出
- 你正在添加新的快照测试
- 预期输出已合法更改
内联快照
对于较小的值,你可以使用 .toMatchInlineSnapshot() 进行内联快照。这些快照直接存储在你的测试文件中:
ts
import { test, expect } from "bun:test";
test("内联快照", () => {
// 第一次运行:快照将自动插入
expect({ hello: "world" }).toMatchInlineSnapshot();
});第一次运行后,Bun 自动更新你的测试文件:
ts
import { test, expect } from "bun:test";
test("内联快照", () => {
expect({ hello: "world" }).toMatchInlineSnapshot(`
{
"hello": "world",
}
`);
});使用内联快照
- 使用
.toMatchInlineSnapshot()编写测试 - 运行测试一次
- Bun 自动使用快照更新你的测试文件
- 在后续运行中,值将与内联快照进行比较
内联快照对于小而简单的值特别有用,在测试文件中直接看到预期输出很有帮助。
错误快照
你还可以使用 .toThrowErrorMatchingSnapshot() 和 .toThrowErrorMatchingInlineSnapshot() 对错误消息进行快照:
ts
import { test, expect } from "bun:test";
test("错误快照", () => {
expect(() => {
throw new Error("Something went wrong");
}).toThrowErrorMatchingSnapshot();
expect(() => {
throw new Error("Another error");
}).toThrowErrorMatchingInlineSnapshot();
});运行后,内联版本变为:
ts
test("错误快照", () => {
expect(() => {
throw new Error("Something went wrong");
}).toThrowErrorMatchingSnapshot();
expect(() => {
throw new Error("Another error");
}).toThrowErrorMatchingInlineSnapshot(`"Another error"`);
});高级快照用法
复杂对象
快照适用于复杂的嵌套对象:
ts
import { test, expect } from "bun:test";
test("复杂对象快照", () => {
const user = {
id: 1,
name: "John Doe",
email: "john@example.com",
profile: {
age: 30,
preferences: {
theme: "dark",
notifications: true,
},
},
tags: ["developer", "javascript", "bun"],
};
expect(user).toMatchSnapshot();
});数组快照
数组也适合快照测试:
ts
import { test, expect } from "bun:test";
test("数组快照", () => {
const numbers = [1, 2, 3, 4, 5].map(n => n * 2);
expect(numbers).toMatchSnapshot();
});函数输出快照
对函数输出进行快照:
ts
import { test, expect } from "bun:test";
function generateReport(data: any[]) {
return {
total: data.length,
summary: data.map(item => ({ id: item.id, name: item.name })),
timestamp: "2024-01-01", // 测试时固定
};
}
test("报告生成", () => {
const data = [
{ id: 1, name: "Alice", age: 30 },
{ id: 2, name: "Bob", age: 25 },
];
expect(generateReport(data)).toMatchSnapshot();
});React 组件快照
快照对于 React 组件特别有用:
tsx
import { test, expect } from "bun:test";
import { render } from "@testing-library/react";
function Button({ children, variant = "primary" }) {
return <button className={`btn btn-${variant}`}>{children}</button>;
}
test("Button 组件快照", () => {
const { container: primary } = render(<Button>Click me</Button>);
const { container: secondary } = render(<Button variant="secondary">Cancel</Button>);
expect(primary.innerHTML).toMatchSnapshot();
expect(secondary.innerHTML).toMatchSnapshot();
});属性匹配器
对于在测试运行之间变化的值(如时间戳或 ID),使用属性匹配器:
ts
import { test, expect } from "bun:test";
test("具有动态值的快照", () => {
const user = {
id: Math.random(), // 每次运行都会变化
name: "John",
createdAt: new Date().toISOString(), // 这也会变化
};
expect(user).toMatchSnapshot({
id: expect.any(Number),
createdAt: expect.any(String),
});
});快照将存储:
txt
exports[`snapshot with dynamic values 1`] = `
{
"createdAt": Any<String>,
"id": Any<Number>,
"name": "John",
}
`;自定义序列化器
你可以自定义对象在快照中的序列化方式:
ts
import { test, expect } from "bun:test";
// Date 对象的自定义序列化器
expect.addSnapshotSerializer({
test: val => val instanceof Date,
serialize: val => `"${val.toISOString()}"`,
});
test("自定义序列化器", () => {
const event = {
name: "Meeting",
date: new Date("2024-01-01T10:00:00Z"),
};
expect(event).toMatchSnapshot();
});最佳实践
保持快照小巧
ts
// 好:专注的快照
test("用户姓名格式化", () => {
const formatted = formatUserName("john", "doe");
expect(formatted).toMatchInlineSnapshot(`"John Doe"`);
});
// 避免:难以审查的巨大快照
test("整个页面渲染", () => {
const page = renderEntirePage();
expect(page).toMatchSnapshot(); // 这可能是数千行
});使用描述性测试名称
ts
// 好:清楚快照代表什么
test("使用 USD 符号格式化货币", () => {
expect(formatCurrency(99.99)).toMatchInlineSnapshot(`"$99.99"`);
});
// 避免:不清楚测试的是什么
test("格式化测试", () => {
expect(format(99.99)).toMatchInlineSnapshot(`"$99.99"`);
});分组相关快照
ts
import { describe, test, expect } from "bun:test";
describe("Button 组件", () => {
test("主要变体", () => {
expect(render(<Button variant="primary">Click</Button>))
.toMatchSnapshot();
});
test("次要变体", () => {
expect(render(<Button variant="secondary">Cancel</Button>))
.toMatchSnapshot();
});
test("禁用状态", () => {
expect(render(<Button disabled>Disabled</Button>))
.toMatchSnapshot();
});
});处理动态数据
ts
// 好:规范化动态数据
test("API 响应格式", () => {
const response = {
data: { id: 1, name: "Test" },
timestamp: Date.now(),
requestId: generateId(),
};
expect({
...response,
timestamp: "TIMESTAMP",
requestId: "REQUEST_ID",
}).toMatchSnapshot();
});
// 或使用属性匹配器
test("使用匹配器的 API 响应", () => {
const response = getApiResponse();
expect(response).toMatchSnapshot({
timestamp: expect.any(Number),
requestId: expect.any(String),
});
});管理快照
审查快照更改
当快照更改时,仔细审查它们:
bash
# 查看更改内容
git diff __snapshots__/
# 如果更改是有意的则更新
bun test --update-snapshots
# 提交更新的快照
git add __snapshots__/
git commit -m "UI 更改后更新快照"清理未使用的快照
Bun 将警告未使用的快照:
txt
Warning: 1 unused snapshot found:
my-test.test.ts.snap: "old test that no longer exists 1"通过从快照文件中删除它们或在可用时使用清理标志运行测试来删除未使用的快照。
组织大型快照文件
对于大型项目,考虑组织测试以保持快照文件易于管理:
text
tests/
├── components/
│ ├── Button.test.tsx
│ └── __snapshots__/
│ └── Button.test.tsx.snap
├── utils/
│ ├── formatters.test.ts
│ └── __snapshots__/
│ └── formatters.test.ts.snap故障排除
快照失败
当快照失败时,你将看到差异:
diff
- Expected
+ Received
Object {
- "name": "John",
+ "name": "Jane",
}常见原因:
- 故意更改(使用
--update-snapshots更新) - 无意的更改(修复代码)
- 动态数据(使用属性匹配器)
- 环境差异(规范化数据)
平台差异
注意特定于平台的差异:
ts
// Windows/Unix 之间的路径可能不同
test("文件操作", () => {
const result = processFile("./test.txt");
expect({
...result,
path: result.path.replace(/\\/g, "/"), // 规范化路径
}).toMatchSnapshot();
});