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/on-console-log.md.
close
  • 简体中文
  • onConsoleLog

    • 类型: (content: string, type: 'stdout' | 'stderr') => boolean | void
    • 默认值: undefined

    在 Rstest 打印测试中的 console 输出前拦截日志。返回 false 会屏蔽该条日志;返回 true 或 undefined 则正常输出。

    这是一个全局配置项,只能在根配置中设置,无法在 projects 中按项目覆盖。

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      onConsoleLog: (content, type) => {
        if (type === 'stderr' && content.includes('deprecated')) {
          return false;
        }
      },
    });

    如果只是想屏蔽全部测试日志或仅保留失败用例的日志,无需自定义过滤函数,直接使用 silent 即可。

    Note

    当 disableConsoleIntercept 为 true 时,onConsoleLog 不会生效。被 silent 丢弃的日志也不会传入 onConsoleLog。