Skip to content
Vitest 1
Main Navigation 指南API配置高級
1.6.1
0.34.6

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

外觀

Sidebar Navigation

指南

為什麼使用 Vitest

開始使用

功能特性

工作區

命令列界面

測試過濾器

報告器

覆蓋率

快照

模擬(Mocking)

測試類型

Vitest UI

瀏覽器模式

原始碼測試

測試上下文

測試環境

擴展匹配器

IDE 整合支援

偵錯

與其他測試執行器的比較

遷移指南

常見錯誤

提升效能

API

測試 API 參考文件

模擬函數

Vi

expect

expectTypeOf

assert

assertType

配置

管理 Vitest 配置文件

配置 Vitest

本頁導覽

報告器 ​

Vitest 提供了幾個內建的報告器,能以不同格式顯示測試結果,並且允許使用自訂報告器。你可以透過 --reporter 命令列選項,或是在 設定檔 中加入 reporters 屬性來選擇不同的報告器。若未指定報告器,Vitest 會使用預設的 default 報告器,詳情如下。

透過命令列使用報告器:

bash
npx vitest --reporter=verbose

透過 vitest.config.ts 使用報告器:

ts
/// <reference types="vitest" />
import { defineConfig } from 'vite';

export default defineConfig({
  test: {
    reporters: ['verbose'],
  },
});

部分報告器可以透過傳遞額外選項進行自訂。報告器特定的選項會在下面的章節中說明。

TIP

自 Vitest v1.3.0 起

ts
export default defineConfig({
  test: {
    reporters: ['default', ['junit', { suiteName: 'UI tests' }]],
  },
});

報告器輸出 ​

預設情況下,Vitest 報告器會將輸出列印至終端機。使用 json、html 或 junit 報告器時,可以透過在 Vite 設定檔或 CLI 中加入 outputFile 設定選項,將測試輸出寫入檔案。

bash
npx vitest --reporter=json --outputFile=./test-output.json
ts
export default defineConfig({
  test: {
    reporters: ['json'],
    outputFile: './test-output.json',
  },
});

組合報告器 ​

你可以同時使用多個報告器,以不同的格式列印測試結果。例如:

bash
npx vitest --reporter=json --reporter=default
ts
export default defineConfig({
  test: {
    reporters: ['json', 'default'],
    outputFile: './test-output.json',
  },
});

上述範例會以預設樣式將測試結果列印至終端機,並以 JSON 格式寫入至指定的輸出檔案。

使用多個報告器時,也可以指定多個輸出檔案,如下所示:

ts
export default defineConfig({
  reporters: ['junit', 'json', 'verbose'],
  outputFile: {
    junit: './junit-report.xml',
    json: './json-report.json',
  },
});

這個範例會分別寫入 JSON 和 XML 報告,並將詳細報告列印至終端機。

內建報告器 ​

預設報告器 ​

預設情況下 (即未指定報告器時),Vitest 會在每個測試套件執行時,以階層式方式顯示結果,並在套件通過後折疊。所有測試執行完畢後,最終的終端機輸出會顯示結果摘要及任何失敗測試的詳細資訊。

測試進行中的範例輸出:

bash
✓ __tests__/file1.test.ts (2) 725ms
✓ __tests__/file2.test.ts (5) 746ms
  ✓ second test file (2) 746ms
    ✓ 1 + 1 should equal 2
    ✓ 2 - 1 should equal 1

測試完成後的最終輸出:

bash
✓ __tests__/file1.test.ts (2) 725ms
✓ __tests__/file2.test.ts (2) 746ms

 Test Files  2 passed (2)
      Tests  4 passed (4)
   Start at  12:34:32
   Duration  1.26s (transform 35ms, setup 1ms, collect 90ms, tests 1.47s, environment 0ms, prepare 267ms)

基本報告器 ​

basic 報告器會顯示已執行的測試檔案,以及在整個套件執行完成後顯示結果摘要。除非個別測試失敗,否則不會將個別測試包含於報告中。

bash
npx vitest --reporter=basic
ts
export default defineConfig({
  test: {
    reporters: ['basic'],
  },
});

使用基本報告器的範例輸出:

bash
✓ __tests__/file1.test.ts (2) 725ms
✓ __tests__/file2.test.ts (2) 746ms

 Test Files  2 passed (2)
      Tests  4 passed (4)
   Start at  12:34:32
   Duration  1.26s (transform 35ms, setup 1ms, collect 90ms, tests 1.47s, environment 0ms, prepare 267ms)

詳細報告器 ​

與 default 報告器相同,採用層級結構,但不折疊已通過測試套件的子樹狀結構。最終的終端機輸出會顯示所有已執行的測試,包括已通過的測試。

bash
npx vitest --reporter=verbose
ts
export default defineConfig({
  test: {
    reporters: ['verbose'],
  },
});

通過測試套件的最終終端機輸出範例:

bash
✓ __tests__/file1.test.ts (2) 725ms
   ✓ first test file (2) 725ms
     ✓ 2 + 2 should equal 4
     ✓ 4 - 2 should equal 2
✓ __tests__/file2.test.ts (2) 746ms
  ✓ second test file (2) 746ms
    ✓ 1 + 1 should equal 2
    ✓ 2 - 1 should equal 1

 Test Files  2 passed (2)
      Tests  4 passed (4)
   Start at  12:34:32
   Duration  1.26s (transform 35ms, setup 1ms, collect 90ms, tests 1.47s, environment 0ms, prepare 267ms)

點狀報告器 ​

為每個已完成的測試列印一個點,以提供最少的輸出,同時仍然顯示所有已執行的測試。僅針對失敗的測試提供詳細資訊,以及套件的 basic 報告器摘要。

bash
npx vitest --reporter=dot
ts
export default defineConfig({
  test: {
    reporters: ['dot'],
  },
});

通過測試套件的範例終端機輸出:

bash
....

 Test Files  2 passed (2)
      Tests  4 passed (4)
   Start at  12:34:32
   Duration  1.26s (transform 35ms, setup 1ms, collect 90ms, tests 1.47s, environment 0ms, prepare 267ms)

JUnit 報告器 ​

以 JUnit XML 格式輸出測試結果的報告。可以使用 outputFile 設定選項,將報告列印至終端機或寫入 XML 檔案。

bash
npx vitest --reporter=junit
ts
export default defineConfig({
  test: {
    reporters: ['junit'],
  },
});

JUnit XML 報告的範例:

xml
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites name="vitest tests" tests="2" failures="1" errors="0" time="0.503">
    <testsuite name="__tests__/test-file-1.test.ts" timestamp="2023-10-19T17:41:58.580Z" hostname="My-Computer.local" tests="2" failures="1" errors="0" skipped="0" time="0.013">
        <testcase classname="__tests__/test-file-1.test.ts" name="first test file &gt; 2 + 2 should equal 4" time="0.01">
            <failure message="expected 5 to be 4 // Object.is equality" type="AssertionError">
AssertionError: expected 5 to be 4 // Object.is equality
 ❯ __tests__/test-file-1.test.ts:20:28
            </failure>
        </testcase>
        <testcase classname="__tests__/test-file-1.test.ts" name="first test file &gt; 4 - 2 should equal 2" time="0">
        </testcase>
    </testsuite>
</testsuites>

輸出的 XML 包含巢狀 testsuites 和 testcase 標籤。你可以使用環境變數 VITEST_JUNIT_SUITE_NAME 和 VITEST_JUNIT_CLASSNAME 來設定它們的 name 和 classname 屬性。這些也可以透過報告器選項自訂:

ts
export default defineConfig({
  test: {
    reporters: [
      [
        'junit',
        { suiteName: 'custom suite name', classname: 'custom-classname' },
      ],
    ],
  },
});

JSON 報告器 ​

以 JSON 格式輸出測試結果的報告。可以使用 outputFile 設定選項,將報告列印至終端機或寫入檔案。

bash
npx vitest --reporter=json
ts
export default defineConfig({
  test: {
    reporters: ['json'],
  },
});

JSON 報告的範例:

json
{
  "numTotalTestSuites": 1,
  "numPassedTestSuites": 0,
  "numFailedTestSuites": 1,
  "numPendingTestSuites": 0,
  "numTotalTests": 1,
  "numPassedTests": 0,
  "numFailedTests": 1,
  "numPendingTests": 0,
  "numTodoTests": 0,
  "startTime": 1697737019307,
  "success": false,
  "testResults": [
    {
      "assertionResults": [
        {
          "ancestorTitles": ["", "first test file"],
          "fullName": " first test file 2 + 2 should equal 4",
          "status": "failed",
          "title": "2 + 2 should equal 4",
          "duration": 9,
          "failureMessages": ["expected 5 to be 4 // Object.is equality"],
          "location": {
            "line": 20,
            "column": 28
          }
        }
      ],
      "startTime": 1697737019787,
      "endTime": 1697737019797,
      "status": "failed",
      "message": "",
      "name": "/root-directory/__tests__/test-file-1.test.ts"
    }
  ]
}

HTML 報告器 ​

產生 HTML 檔案,以便透過互動式 GUI 檢視測試結果。產生檔案後,Vitest 會保持本機開發伺服器運行,並提供連結以便在瀏覽器中檢視報告。

可以使用 outputFile 設定選項指定輸出檔案。若未提供 outputFile 選項,則會建立新的 HTML 檔案。

bash
npx vitest --reporter=html
ts
export default defineConfig({
  test: {
    reporters: ['html'],
  },
});

TIP

此報告器需要安裝 @vitest/ui 套件。

TAP 報告器 ​

輸出遵循 Test Anything Protocol (TAP) 協定的報告。

bash
npx vitest --reporter=tap
ts
export default defineConfig({
  test: {
    reporters: ['tap'],
  },
});

TAP 報告的範例:

bash
TAP version 13
1..1
not ok 1 - __tests__/test-file-1.test.ts # time=14.00ms {
    1..1
    not ok 1 - first test file # time=13.00ms {
        1..2
        not ok 1 - 2 + 2 should equal 4 # time=11.00ms
            ---
            error:
                name: "AssertionError"
                message: "expected 5 to be 4 // Object.is equality"
            at: "/root-directory/__tests__/test-file-1.test.ts:20:28"
            actual: "5"
            expected: "4"
            ...
        ok 2 - 4 - 2 should equal 2 # time=1.00ms
    }
}

TAP 平面報告器 ​

與 tap 報告器相同,測試結果的格式設定為遵循 TAP 標準,但測試套件的格式設定為平面清單,而非巢狀階層。

bash
npx vitest --reporter=tap-flat
ts
export default defineConfig({
  test: {
    reporters: ['tap-flat'],
  },
});

TAP 平面報告的範例:

bash
TAP version 13
1..2
not ok 1 - __tests__/test-file-1.test.ts > first test file > 2 + 2 should equal 4 # time=11.00ms
    ---
    error:
        name: "AssertionError"
        message: "expected 5 to be 4 // Object.is equality"
    at: "/root-directory/__tests__/test-file-1.test.ts:20:28"
    actual: "5"
    expected: "4"
    ...
ok 2 - __tests__/test-file-1.test.ts > first test file > 4 - 2 should equal 2 # time=0.00ms

懸掛程序報告器 ​

hanging-process 報告器本身不會顯示測試結果,但可以與其他報告器結合使用,以便在測試執行時監控程序。使用此報告器可能會消耗大量資源,因此通常應保留用於 Vitest 無法持續結束程序的情況下進行除錯。

bash
npx vitest --reporter=hanging-process
ts
export default defineConfig({
  test: {
    reporters: ['hanging-process'],
  },
});

GitHub Actions 報告器 1.3.0+ ​

輸出 工作流程命令,以針對測試失敗提供註記。當 process.env.GITHUB_ACTIONS === 'true' 時,會自動啟用此報告器,並搭配 default 報告器使用。

若您設定非預設的報告器,則需要明確新增 github-actions。

ts
export default defineConfig({
  test: {
    reporters: process.env.GITHUB_ACTIONS ? ['dot', 'github-actions'] : ['dot'],
  },
});
Github ActionsGithub Actions

自訂報告器 ​

您可以透過在報告器的選項中指定套件名稱,來使用從 NPM 安裝的第三方自訂報告器。

bash
npx vitest --reporter=some-published-vitest-reporter
ts
export default defineConfig({
  test: {
    reporters: ['some-published-vitest-reporter'],
  },
});

此外,您可以定義自己的 自訂報告器,並透過指定檔案路徑來使用。

bash
npx vitest --reporter=./path/to/reporter.ts

自訂報告器應實現 Reporter 介面。

Pager
上一頁測試過濾器
下一頁覆蓋率

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors

https://v1.vitest.dev/guide/reporters

以 MIT 授權條款 發布。

版權所有 (c) 2024 Mithril Contributors