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/guide/debug/debugging.md.
close
  • 简体中文
  • 调试

    开启调试模式

    通过设置 DEBUG=rstest 环境变量开启调试模式(支持以逗号分隔的 rstest、rsbuild、builder 或 *,不区分大小写):

    DEBUG=rstest pnpm test

    在调试模式下,Rstest 会:

    • 将测试临时构建产物输出到磁盘
    • 将最终生效的 Rstest、Rsbuild 与 Rspack 配置写入磁盘
    • 打印完整的错误堆栈信息
    • 将日志级别设置为 verbose,并在 watch 模式下关闭清屏

    Rstest 配置文件

    开启调试模式后,Rstest 会将最终配置输出到 dist/.rstest-temp/.rsbuild/ 目录(如果配置了 output.distPath.root,则位于 <output.distPath.root>/.rsbuild/):

    • rstest.config.mjs:最终解析后的 Rstest 配置
    • rsbuild.config.mjs:生成的 Rsbuild 配置
    • rspack.config.<project>.mjs:各项目对应的 Rspack 配置(例如默认项目为 rspack.config.rstest.mjs)

    默认配置下,dist/.rstest-temp/.rsbuild/rstest.config.mjs 的内容大致如下:

    dist/.rstest-temp/.rsbuild/rstest.config.mjs
    export default {
      root: '/path/to/project',
      name: 'rstest',
      include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'],
      // ...
    };

    完整配置项说明见配置 Rstest。

    查看测试中间产物

    在调试模式下或手动开启 dev.writeToDisk 时,Rstest 会将编译后的测试 bundle 和 source map 写入 dist/.rstest-temp(或 output.distPath.root)目录,可直接检查产物定位构建问题。

    查看构建产物的覆盖率

    如果 Node.js 测试的 bundle 比预期大,可以开启 V8 覆盖率 并设置 DEBUG=rstest:bundle-coverage,查看测试运行器加载了哪些构建产物以及实际执行的代码范围:

    DEBUG=rstest:bundle-coverage pnpm rstest run --coverage --coverage.provider v8

    Rstest 会在项目根目录的 .rstest 下写入 bundle-coverage-<timestamp>.json,并在终端打印文件路径。每个测试文件条目中,assets 记录各构建产物的字节数,rawV8 记录 source map 重映射前的 V8 执行范围。若只需排查单个 bundle,可直接给 rstest run 传入测试文件名。

    被 rs.mock() 完全替换的依赖仍可能被打包,可通过 output.externals 将其排除在 bundle 之外。

    如果你的 Coding Agent 支持 Skills,可以先安装 rstest-debugging,再使用下面的 prompt:

    npx skills add rstackjs/agent-skills --skill rstest-debugging
    For your Agent
    排查构建产物覆盖率

    复制这段 prompt 并发给你的 Coding Agent。

    调试测试 worker

    为避免调试端口冲突,Rstest 启动 worker 时会过滤主进程上的 --inspect* 和 --debug-port 参数。直接执行 node --inspect ./node_modules/@rstest/core/bin/rstest.js 只会附加到 Rstest 主进程。

    若要调试测试 worker 中的代码,请通过 pool.execArgv 传入 --inspect-brk,同时用 --pool.maxWorkers=1 将并发限制为单 worker,并用 --testTimeout=0 关闭超时限制:

    npx rstest run --pool.execArgv=--inspect-brk --pool.maxWorkers=1 --testTimeout=0

    当 pool.execArgv 包含 --inspect*(或主进程处于调试状态)时,Rstest 会自动启用内联 source map(inline-nosources-source-map)并将产物分片上限设为 1 MB,确保断点能准确映射回源码。

    调试浏览器模式

    调试 Browser Mode 测试时,使用 --browser.headless=false 可以打开可见的浏览器窗口(例如在 CI 或已配置 browser.headless: true 的项目中),方便直接查看 DOM 状态和开发者工具:

    npx rstest run --browser.headless=false

    当 browser.headless 为 true(CI 默认值)时,测试失败会自动将 PNG 截图保存到 .rstest/screenshots/,可通过 browser.screenshotFailures 关闭。

    调试异步泄漏与内存

    • --detectAsyncLeaks:在测试文件结束后检测仍未关闭的异步资源(例如未清理的定时器),报错并打印资源创建堆栈。见 detectAsyncLeaks。
    • --logHeapUsage:在每个测试文件与用例后打印 V8 heapUsed 内存用量(仅适用于 Node.js pool)。见 logHeapUsage。

    在 VS Code 中调试

    打开 VS Code 的 JavaScript Debug Terminal 并运行测试命令,即可直接使用 debugger 语句或编辑器断点调试。

    也可以在 .vscode/launch.json 中添加启动配置,通过 F5 或 Run and Debug 面板调试当前打开的测试文件:

    .vscode/launch.json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Debug Current Test File",
          "type": "node",
          "request": "launch",
          "program": "${workspaceFolder}/node_modules/@rstest/core/bin/rstest.js",
          "args": ["run", "${file}"],
          "console": "integratedTerminal",
          "internalConsoleOptions": "neverOpen",
          "skipFiles": ["<node_internals>/**"]
        }
      ]
    }