unit-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnit Tests
单元测试
Act as a top-tier software engineer with serious testing skills.
Write unit tests for: $ARGUMENTS
Each test must answer these 5 questions:
- What is the unit under test? (test should be in a named describe block)
- What is the expected behavior? ($given and $should arguments are adequate)
- What is the actual output? (the unit under test was exercised by the test)
- What is the expected output? ($expected and/or $should are adequate)
- How can we find the bug? (implicitly answered if the above questions are answered correctly)
扮演一名拥有出色测试技能的顶尖软件工程师。
为以下内容编写单元测试:$ARGUMENTS
每个测试必须回答以下5个问题:
- 被测单元是什么?(测试应放在命名的describe块中)
- 预期行为是什么?($given和$should参数已足够说明)
- 实际输出是什么?(测试中已执行被测单元)
- 预期输出是什么?($expected和/或$should已足够说明)
- 如何定位bug?(如果正确回答了上述问题,该问题会被间接解答)
Rules
规则
- Use Vitest with describe, expect, and test.
- Tests must use the "given: ..., should: ..." prose format.
- Colocate tests with functions. Test files should be in the same folder as the implementation file.
- Use cuid2 for IDs unless specified otherwise.
- If an argument is a database entity, use an existing factory function and override values as needed.
- Capture the and the
actualvalue in variables.expected - The top-level block should describe the component under test.
describe - The block should describe the case via
test."given: ..., should: ..." - Avoid in assertions. Expect specific values instead.
expect.any(Constructor) - Always use the equality assertion.
toEqual - Empty line before variable assignment.
actual - NO empty line between and
actualassignments.expected - Empty line after variable assignment before the
expectedassertion.toEqual - Tests must be readable, isolated, thorough, and explicit.
- For integration tests, test integration with the real system.
- Test expected edge cases.
- Create factory functions for reused data structures rather than sharing mutable fixtures.
- 使用Vitest的describe、expect和test方法。
- 测试必须采用“given: ..., should: ...”的叙述格式。
- 测试文件与函数同目录存放。测试文件应和实现文件放在同一个文件夹中。
- 除非另有指定,否则使用cuid2生成ID。
- 如果参数是数据库实体,使用现有的工厂函数并根据需要覆盖值。
- 将和
actual值存入变量。expected - 顶层的块应描述被测组件。
describe - 块应通过
test描述测试场景。"given: ..., should: ..." - 在断言中避免使用,而是期待具体的值。
expect.any(Constructor) - 始终使用相等断言。
toEqual - 在变量赋值前留空行。
actual - 和
actual赋值之间不要留空行。expected - 在变量赋值后、
expected断言前留空行。toEqual - 测试必须可读、独立、全面且明确。
- 对于集成测试,测试与真实系统的集成情况。
- 测试预期的边缘情况。
- 为复用的数据结构创建工厂函数,而非共享可变的测试夹具。