unit-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unit Tests

单元测试

Act as a top-tier software engineer with serious testing skills.
Write unit tests for: $ARGUMENTS
Each test must answer these 5 questions:
  1. What is the unit under test? (test should be in a named describe block)
  2. What is the expected behavior? ($given and $should arguments are adequate)
  3. What is the actual output? (the unit under test was exercised by the test)
  4. What is the expected output? ($expected and/or $should are adequate)
  5. How can we find the bug? (implicitly answered if the above questions are answered correctly)
扮演一名拥有出色测试技能的顶尖软件工程师。
为以下内容编写单元测试:$ARGUMENTS
每个测试必须回答以下5个问题:
  1. 被测单元是什么?(测试应放在命名的describe块中)
  2. 预期行为是什么?($given和$should参数已足够说明)
  3. 实际输出是什么?(测试中已执行被测单元)
  4. 预期输出是什么?($expected和/或$should已足够说明)
  5. 如何定位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
    actual
    and the
    expected
    value in variables.
  • The top-level
    describe
    block should describe the component under test.
  • The
    test
    block should describe the case via
    "given: ..., should: ..."
    .
  • Avoid
    expect.any(Constructor)
    in assertions. Expect specific values instead.
  • Always use the
    toEqual
    equality assertion.
  • Empty line before
    actual
    variable assignment.
  • NO empty line between
    actual
    and
    expected
    assignments.
  • Empty line after
    expected
    variable assignment before the
    toEqual
    assertion.
  • 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
    断言前留空行。
  • 测试必须可读、独立、全面且明确。
  • 对于集成测试,测试与真实系统的集成情况。
  • 测试预期的边缘情况。
  • 为复用的数据结构创建工厂函数,而非共享可变的测试夹具。