命令行工具

Rstest 提供了一个轻量级的命令行工具,包含 rstest watchrstest run 等命令。

rstest -h

rstest -h 可帮助你查看所有可用的 CLI 命令及选项:

npx rstest -h

输出如下:

Usage:
  $ rstest [...filters]

Commands:
  [...filters]        run tests
  run [...filters]    run tests without watch mode
  watch [...filters]  run tests in watch mode
  list [...filters]   lists all test files that Rstest will run

Options:
  -w, --watch                              Run tests in watch mode
  -h, --help                               Display this message
  -v, --version                            Display version number
  -c, --config <config>
  ...

rstest [...filters]

直接运行 rstest 命令将会在当前目录执行 Rstest 测试。

$ npx rstest

 test/index.test.ts (2 tests) 1ms

  Test Files 1 passed (1)
       Tests 2 passed (2)
    Duration 189 ms (build 22 ms, tests 167 ms)

Watch 模式

如果你希望在文件更改时自动重新运行测试,可以使用 --watchrstest watch 命令:

$ npx rstest --watch

rstest run

rstest run 将会执行单次测试,该命令适用于 CI 环境或不需要一边修改一边执行测试的场景。

rstest watch

rstest watch 将会启动监听模式并执行测试,当测试或依赖文件修改时,将重新执行关联的测试文件。

rstest list

rstest list 将会打印所有匹配条件的测试列表。默认情况下,它将打印所有匹配条件的测试名称。

$ npx rstest list

# 输出如下:
a.test.ts > test a > test a-1
a.test.ts > test a-2
b.test.ts > test b > test b-1
b.test.ts > test b-2

rstest list 命令继承所有 rstest 过滤选项,你可以直接过滤文件或使用 -t 过滤指定的测试名称。

$ npx rstest list -t='test a'

# 输出如下:
a.test.ts > test a > test a-1
a.test.ts > test a-2

你可以使用 --filesOnly 使其仅打印测试文件:

$ npx rstest list --filesOnly

# 输出如下:
a.test.ts
b.test.ts

你可以使用 --json 使其以 JSON 格式打印测试或将结果保存到单独的文件中:

$ npx rstest list --json

$ npx rstest list --json=./output.json

CLI 选项

Rstest CLI 支持以下常用参数,所有命令均可使用:

参数说明
-c, --config <config>指定配置文件路径(相对或绝对路径),详见 指定配置文件
--config-loader <loader>指定配置加载器,可选 jitinative,详见 Rsbuild - 指定加载方式
-r, --root <root>指定项目根目录,详见 root
--globals提供全局 API,详见 globals
--isolate在隔离环境中运行测试,详见 isolate
--exclude <exclude>排除指定文件,详见 exclude
-u, --update更新快照文件,详见 update
--passWithNoTests当未找到测试文件时允许测试通过,详见 passWithNoTests
--printConsoleTrace调用 console 方法时打印调用栈,详见 printConsoleTrace
--disableConsoleIntercept禁用 console 拦截,详见 disableConsoleIntercept
--slowTestThreshold <value>设置测试或套件被视为慢的阈值(毫秒),详见 slowTestThreshold
-t, --testNamePattern <value>仅运行名称匹配正则的测试,详见 testNamePattern
--testEnvironment <name>指定测试环境,详见 testEnvironment
--testTimeout <value>设置单个测试的超时时间(毫秒),详见 testTimeout
--hookTimeout <value>设置单个测试 hook 的超时时间(毫秒),详见 hookTimeout
--retry <retry>测试失败时重试次数,详见 retry
--reporter <reporter>指定测试报告器,详见 reporters
--maxConcurrency <value>最大并发测试数,详见 maxConcurrency
--clearMocks每个测试前自动清除 mock 调用、实例、上下文和结果,详见 clearMocks
--resetMocks每个测试前自动重置 mock 状态,详见 resetMocks
--restoreMocks每个测试前自动恢复 mock 状态和实现,详见 restoreMocks
--unstubGlobals每个测试前恢复被 rstest.stubGlobal 修改的全局变量,详见 unstubGlobals
--unstubEnvs每个测试前恢复被 rstest.stubEnv 修改的 process.env,详见 unstubEnvs
-h, --help显示帮助信息
-v, --version显示版本号

CLI 快捷键

在 watch 模式下运行 Rstest 时,你可以使用键盘快捷键来执行各种便捷操作。

所有快捷键:

  Shortcuts:
  f  rerun failed tests
  a  rerun all tests
  u  update snapshot
  t  filter by a test name regex pattern
  p  filter by a filename regex pattern
  q  quit process
  c  clear console
  h  show shortcuts help
NOTE

CLI 快捷键仅在 watch 模式下运行 Rstest(rstest watchrstest --watch)且终端支持 TTY(交互模式)时可用。