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

Test Configurations

root
name
include
exclude
setupFiles
projects
update
globals
passWithNoTests
includeSource
testNamePattern
env
retry
testTimeout
hookTimeout
maxConcurrency
pool
isolate
testEnvironment
clearMocks
resetMocks
restoreMocks
unstubEnvs
unstubGlobals
coverage
reporters
hideSkippedTests
slowTestThreshold
snapshotFormat
resolveSnapshotPath
printConsoleTrace
onConsoleLog
disableConsoleIntercept

Build Configurations

plugins
source
output
resolve
tools
dev
performance
📝 在 GitHub 上编辑此页
上一页coverage
下一页hideSkippedTests

#reporters

  • 类型:
type Reporter = ReporterName | [ReporterName, ReporterOptions];

type Reporters = Reporter | Reporter[];
  • 默认值:
process.env.GITHUB_ACTIONS === 'true'
  ? ['default', 'github-actions']
  : ['default'];
  • CLI: --reporter=<name> --reporter=<name1>

自定义输出报告器的类型。

#Built-in Reporters

#Default reporter

默认情况下,Rstest 会在终端显示测试运行状态、结果以及汇总信息。

输出如下:

 ✓ test/index.test.ts (2)

 Test Files 1 passed
      Tests 2 passed
   Duration 112ms (build 19ms, tests 93ms)

#Verbose reporter

默认报告器仅在测试运行失败或耗时缓慢时输出相关的测试用例信息,Verbose 报告器会在测试完成后输出所有的测试用例信息。

CLI
rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  reporters: 'verbose'
});

此时,Rstest 输出如下:

 ✓ test/index.test.ts (2) 2ms
  ✓ Index > should add two numbers correctly (1ms)
  ✓ Index > should test source code correctly (1ms)

 Test Files 1 passed
      Tests 2 passed
   Duration 112ms (build 19ms, tests 93ms)

#Github actions reporter

Github Actions 报告器将在测试失败时以 workflow commands 的形式输出错误信息。

#输出示例

当测试失败时,Github Actions 报告器会输出类似以下格式的信息:

::error file=src/index.ts,line=4,col=17,title=test/index.test.ts > should add two numbers correctly::expected 2 to be 4

这些输出会被 GitHub Actions 解析,并在对应的位置生成注释。

rstest-github-actions-example

#自动启用

当未手动设置任何 reporter 时,Rstest 会在检测到 GitHub Actions 环境(process.env.GITHUB_ACTIONS 为 'true')时自动启用此报告器。

#手动启用

你也可以手动启用此报告器:

CLI
rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  reporters: ['github-actions']
});

#JUnit reporter

JUnit 报告器支持以 JUnit XML 格式输出测试报告,方便与 CI/CD 系统集成。

CLI
rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  reporters: ['junit']
});

#配置项

  • outputPath: 配置报告输出的路径,如果不指定则输出到控制台。
rstest.config.ts
import { defineConfig } from '@rstest/core';

export default defineConfig({
  reporters: [['junit', { outputPath: './junit.xml' }]],
});

#输出示例

JUnit 报告器生成的 XML 格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="rstest tests" tests="3" failures="1" errors="1" skipped="1" time="0.3" timestamp="2024-01-01T00:00:00.000Z">
  <testsuite name="test1.test.ts" tests="3" failures="1" errors="1" skipped="1" time="0.3" timestamp="2024-01-01T00:00:00.000Z">
    <testcase name="should pass" classname="test1.test.ts" time="0.1">
    </testcase>
    <testcase name="should fail" classname="test1.test.ts" time="0.2">
      <failure message="Test failed" type="AssertionError">expected 'hi' to be 'hii' // Object.is equality - Expected + Received - hii + hi at test1.test.ts:10:21</failure>
    </testcase>
    <testcase name="should skip" classname="test1.test.ts" time="0.0">
      <skipped/>
    </testcase>
  </testsuite>
</testsuites>

JUnit 报告器会根据测试用例的执行状态,将其映射为 JUnit 测试状态:

  • pass: 测试通过
  • fail: 测试失败,会生成 <failure> 标签
  • skip: 测试跳过,会生成 <skipped> 标签
  • todo: 测试待办,会生成 <skipped> 标签