close
logo
Rstest
指南
配置
API
English
简体中文
指南
配置
API
English
简体中文
logo
Rstest

开始

介绍
快速上手
功能导航

基础

命令行工具
配置 Rstest
过滤测试
VS Code 扩展

进阶

调试
性能分析

迁移

从 Jest 迁移
📝 在 GitHub 上编辑此页
上一页VS Code 扩展
下一页性能分析

#调试

#开启调试模式

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

DEBUG=rstest pnpm test

在调试模式下,Rstest 会:

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

#Rstest 配置文件

在调试模式下,Rstest 会自动生成 dist/.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 章节。

#在 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 面板来启动调试。