For AI agents: the complete documentation index is available at https://rstest.rs/zh/llms.txt, the full documentation bundle is available at https://rstest.rs/zh/llms-full.txt, and this page is available as Markdown at https://rstest.rs/zh/config/test/reporters.md.
close
  • 简体中文
  • reporters

    • 类型:
    type ReporterConfig =
      | BuiltInReporterNames
      | (string & {})
      | [name: string, options: Record<string, unknown>]
      | Reporter;
    type Reporters = ReporterConfig | ReporterConfig[];
    process.env.GITHUB_ACTIONS === 'true'
      ? ['default', 'github-actions']
      : ['default'];
    • CLI: --reporters=<name> --reporters=<name1>(同时兼容 --reporter 作为别名)

    配置用于测试结果输出的报告器。

    内置报告器名称包括 default、dot、verbose、md、github-actions、junit、json 和 blob。

    AI agent 环境

    未显式配置 reporters(配置文件无 reporters,且 CLI 未传 --reporters)时,Rstest 会默认使用 ['md'],将更适合 AI agent 的 markdown 输出到 stdout。

    使用方法

    基本示例

    你可以在 rstest.config.ts 文件中或通过 CLI 指定报告器。

    CLI
    rstest.config.ts
    npx rstest --reporters=default

    数组形式

    你可以通过数组形式同时指定多个报告器。当你既需要控制台输出又需要 CI/CD 管道的文件报告时,这非常有用。

    CLI
    rstest.config.ts
    npx rstest --reporters=default --reporters=junit

    报告器配置

    当报告器支持配置选项时,可以通过 [reporterName, options] 传递配置选项。

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      reporters: [
        ['default', { verbose: true }],
        ['github-actions', { verbose: true }],
        ['junit', { outputPath: './test-results.xml' }],
      ],
    });

    使用自定义报告器

    自定义报告器可以用模块标识符传入,也可以用 [模块标识符, 选项] 元组把选项一起带上。模块必须默认导出一个实现了 Reporter 接口的类;Rstest 会用选项(只写字符串时是空对象)和包含 rootPath、解析后的 config 的上下文来构造实例。模块可以是 JavaScript 或 TypeScript:在启用了类型擦除的 Node 版本(Node 22.18+ 或 24)上,.ts、.mts 模块可以直接加载:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      reporters: './custom-reporter.js',
    });
    
    // 传入 options:
    export const configWithOptions = defineConfig({
      reporters: [['./custom-reporter.js', { outputPath: './report.json' }]],
    });

    你也可以传入已经创建、实现了 Reporter interface 的对象,但不能直接传入 Reporter class。

    相关文档