Skip to content

test(cli): make error assertions ANSI-safe #175

Description

@luojiyin1987

问题

cli.spec.ts 的多行错误断言在本地(FORCE_COLOR 开启)失败,CI 通过:

Expected: "[lint-md] The following options cannot be used with --stdin:
--threads
--max-file-size"
Received: "[lint-md] The following options cannot be used with --stdin:
--threads
--max-file-size"

肉眼一样,stringContaining() 却失败。

根因

formatCliError() 执行 chalk.red(error.message)。Chalk 对多行字符串会在每个换行处插入 ANSI close/reopen 控制码:

\u001b[31m[lint-md] ...--stdin:\u001b[39m\n\u001b[31m--threads\u001b[39m\n...

Jest 展示时渲染掉了颜色码。CI 无 TTY 时 Chalk 自动关闭颜色,所以测试实际上依赖运行环境。

方案

不改生产代码(#170 实现没问题),改测试:用已有的 strip-ansi 依赖剥离颜色码后做精确断言——这个用例本身就在验证完整的聚合错误格式,精确匹配比 stringContaining 更合适。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions