dust-test
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreating automated tests for Dust codebases
为Dust代码库创建自动化测试
Write focused, practical tests for the current file following the 80/20 principle.
遵循80/20原则,为当前文件编写聚焦、实用的测试。
Instructions
说明
When writing tests for a file:
- Identify the core functionality: Focus on the most important paths and edge cases that provide 80% of the value
- Keep it simple: Write straightforward tests that are easy to understand and maintain
- Minimal mocking:
- DO NOT mock the database
- Only mock external services (APIs, third-party services)
- Prefer real implementations when possible
- Use factories: Leverage test factories to set up data efficiently
- Focus on behavior: Test what the code does, not how it does it
为文件编写测试时:
- 识别核心功能:聚焦于能带来80%价值的最重要路径和边缘情况
- 保持简洁:编写易于理解和维护的简单测试
- 最小化模拟:
- 请勿模拟数据库
- 仅模拟外部服务(API、第三方服务)
- 尽可能优先使用真实实现
- 使用工厂函数:利用测试工厂高效设置数据
- 聚焦行为:测试代码的功能,而非实现方式
For Front (TypeScript/Next.js)
前端(TypeScript/Next.js)
Setup
设置
- Import factories from
front/tests/utils/factories - Import utilities from
front/tests/utils/utils - Use the test database (no mocking)
- 从导入工厂函数
front/tests/utils/factories - 从导入工具函数
front/tests/utils/utils - 使用测试数据库(不模拟)
Structure
结构
typescript
import {describe, it, expect} from "vitest";
import {makeTestWorkspace, makeTestUser} from "tests/utils/factories";
describe ("ComponentName or FunctionName", () => {
it ("should handle the main happy path", async () => {
// Arrange: Set up using factories
const {workspace} = createResourceTest ()
// Act: Execute the code
const result = await functionUnderTest (workspace);
// Assert: Verify behavior
expect (result).toBeDefined ();
});
it ("should handle the most common edge case", async () => {
// Test the second most important scenario
});
});typescript
import {describe, it, expect} from "vitest";
import {makeTestWorkspace, makeTestUser} from "tests/utils/factories";
describe ("ComponentName or FunctionName", () => {
it ("should handle the main happy path", async () => {
// 准备:使用工厂函数设置数据
const {workspace} = createResourceTest ()
// 执行:运行待测试代码
const result = await functionUnderTest (workspace);
// 断言:验证行为
expect (result).toBeDefined ();
});
it ("should handle the most common edge case", async () => {
// 测试第二重要的场景
});
});What to test (80/20 focus)
测试重点(80/20原则)
- Main success paths
- Most common error conditions
- Critical edge cases (null/undefined, empty arrays, etc.)
- Permission checks (if applicable)
- 主要成功路径
- 最常见的错误情况
- 关键边缘情况(null/undefined、空数组等)
- 权限检查(如适用)
What to skip (diminishing returns)
可忽略内容(收益递减)
- Exhaustive parameter combinations
- Unlikely edge cases
- Internal implementation details
- UI component rendering (unless critical)
- 详尽的参数组合
- 罕见的边缘情况
- 内部实现细节
- UI组件渲染(除非是关键组件)
For Connectors/Core
连接器/核心模块
Follow similar principles:
- Use factories appropriate to the service
- Focus on integration points
- Mock external APIs only (Slack, Notion, GitHub, etc.)
- Test the database interactions directly
遵循类似原则:
- 使用适用于对应服务的工厂函数
- 聚焦于集成点
- 仅模拟外部API(Slack、Notion、GitHub等)
- 直接测试数据库交互
Example Pattern
示例模式
typescript
describe ("createConversation", () => {
it ("creates conversation with valid params", async () => {
const {workspace, user} = createResourceTest ()
const conversation = await createConversation ({
workspace,
userId: user.id,
title: "Test"
});
expect (conversation.sId).toBeDefined ();
expect (conversation.title).toBe ("Test");
});
it ("fails without required permissions", async () => {
const {workspace, user} = createResourceTest ()
await expect (
createConversation ({workspace, userId: user.id})
).rejects.toThrow ("Permission denied");
});
});typescript
describe ("createConversation", () => {
it ("creates conversation with valid params", async () => {
const {workspace, user} = createResourceTest ()
const conversation = await createConversation ({
workspace,
userId: user.id,
title: "Test"
});
expect (conversation.sId).toBeDefined ();
expect (conversation.title).toBe ("Test");
});
it ("fails without required permissions", async () => {
const {workspace, user} = createResourceTest ()
await expect (
createConversation ({workspace, userId: user.id})
).rejects.toThrow ("Permission denied");
});
});Execution Steps
执行步骤
- Read the file to understand its purpose and main exports
- Check if a test file already exists (e.g., )
file.test.ts - Identify the 2-4 most important functions/behaviors to test
- Find or create appropriate factories for test data
- Write concise, focused tests
- Run tests with to verify they pass
npm test
- 阅读文件以了解其用途和主要导出内容
- 检查是否已存在测试文件(例如)
file.test.ts - 确定2-4个最重要的待测试函数/行为
- 查找或创建适用于测试数据的工厂函数
- 编写简洁、聚焦的测试
- 运行以验证测试通过
npm test