close
  • 简体中文
  • 调试

    开启调试模式

    为了便于排查问题,Rstest 提供了调试模式,你可以在执行测试时添加 DEBUG=rstest 环境变量来开启 Rstest 的调试模式。

    DEBUG=rstest pnpm test

    在调试模式下,Rstest 会:

    • 将测试中间产物输出到磁盘
    • 将最终生成的 Rstest 配置、Rsbuild 配置和 Rspack 配置写入到产物目录下
    • 打印出完整的错误栈信息
    • 将日志级别设置为 verbose

    Rstest 配置文件

    在调试模式下,Rstest 会自动生成 dist/.rstest-temp/rsbuild/rstest.config.mjs 文件,这里面包含了最终生成的 Rstest 配置。在这个文件里,你可以了解到你传入的 Rstest 配置在经过框架层和 Rstest 处理后的最终结果。

    该文件的大致内容如下:

    rstest.config.mjs
    export default {
      name: 'rstest',
      include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'],
      exclude: [
        '**/node_modules/**',
        '**/dist/**',
        '**/.{idea,git,cache,output,temp}/**',
        '**/dist/.rstest-temp',
      ],
      includeSource: [],
      pool: {
        type: 'forks',
      },
      isolate: true,
      globals: false,
      passWithNoTests: false,
      update: false,
      testTimeout: 5000,
      testEnvironment: 'node',
      retry: 0,
      clearMocks: false,
      resetMocks: false,
      restoreMocks: false,
      slowTestThreshold: 300,
      // other configs...
    };

    关于 Rstest 配置项的完整介绍,请查看配置 Rstest 章节。

    查看测试中间产物

    调试模式 下或者手动开启 dev.writeToDisk 配置项时,Rstest 会将测试中间产物输出到磁盘。你可以通过查看 dist/.rstest-temp 目录下的文件来了解测试中间产物。

    在 VS Code 中调试

    Rstest 支持通过 debugger 语句或断点的方式在 VS Code 中进行调试。只需打开 JavaScript Debug Terminal,然后在终端中运行测试命令即可。

    你也可以添加一个启动配置来调试当前打开的测试文件:

    .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>/**"]
        }
      ]
    }

    之后,你可以直接在 VS Code 中按下 F5 键或前往 Run and Debug 面板来启动调试。