記者
Vitest 提供多個內建記者,以不同格式顯示測試輸出,以及使用自訂記者的功能。你可以使用 --reporter
命令列選項或在 設定檔 中加入 reporters
屬性,來選擇不同的記者。如果沒有指定記者,Vitest 將使用如下所述的 default
記者。
透過命令列使用記者
npx vitest --reporter=verbose
透過 vitest.config.ts
使用記者
/// <reference types="vitest" />
import { } from 'vite'
export default ({
: {
: ['verbose']
},
})
有些記者可以透過傳遞其他選項給他們來客製化。記者特定的選項在以下部分說明。
提示
自 Vitest v1.3.0 起
export default defineConfig({
test: {
reporters: [
'default',
['junit', { suiteName: 'UI tests' }]
],
},
})
記者輸出
預設情況下,Vitest 的記者會將其輸出印到終端機。當使用 json
、html
或 junit
記者時,你可以透過在你的 Vite 組態檔或透過 CLI 中包含一個 outputFile
組態選項,來將你的測試輸出寫入檔案。
npx vitest --reporter=json --outputFile=./test-output.json
export default defineConfig({
test: {
reporters: ['json'],
outputFile: './test-output.json'
},
})
結合記者
你可以同時使用多個記者,以不同格式印出你的測試結果。例如
npx vitest --reporter=json --reporter=default
export default defineConfig({
test: {
reporters: ['json', 'default'],
outputFile: './test-output.json'
},
})
上述範例會同時以預設樣式將測試結果印到終端機,並將它們以 JSON 寫入指定的輸出檔案。
當使用多個記者時,也可以指定多個輸出檔案,如下所示
export default defineConfig({
reporters: ['junit', 'json', 'verbose'],
outputFile: {
junit: './junit-report.xml',
json: './json-report.json',
},
})
此範例會寫入個別的 JSON 和 XML 報告,並將詳細報告印到終端機。
內建記者
預設記者
預設情況下(即未指定記者),Vitest 會在測試套件執行時,以階層方式顯示每個測試套件的結果,然後在套件通過後收合。當所有測試執行完畢後,最終的終端機輸出會顯示結果摘要和任何失敗測試的詳細資料。
正在進行測試的範例輸出
✓ __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
測試完成後的最終輸出
✓ __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
記者會顯示已執行的測試檔案,以及在整個套件執行完畢後的結果摘要。除非測試失敗,否則不會將個別測試包含在報告中。
npx vitest --reporter=basic
export default defineConfig({
test: {
reporters: ['basic']
},
})
使用基本報告器的範例輸出
✓ __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
報告器相同的層級結構,但不會對通過的測試套件摺疊子樹。最後的終端輸出會顯示所有已執行的測試,包括已通過的測試。
npx vitest --reporter=verbose
export default defineConfig({
test: {
reporters: ['verbose']
},
})
通過測試套件的最終終端輸出範例
✓ __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
報告器摘要。
npx vitest --reporter=dot
export default defineConfig({
test: {
reporters: ['dot']
},
})
通過測試套件的終端輸出範例
....
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 檔案。
npx vitest --reporter=junit
export default defineConfig({
test: {
reporters: ['junit']
},
})
JUnit 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 > 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 > 4 - 2 should equal 2" time="0">
</testcase>
</testsuite>
</testsuites>
輸出的 XML 包含巢狀的 testsuites
和 testcase
標籤。您可以使用環境變數 VITEST_JUNIT_SUITE_NAME
和 VITEST_JUNIT_CLASSNAME
分別設定它們的 name
和 classname
屬性。這些也可以透過報告器選項自訂
export default defineConfig({
test: {
reporters: [
['junit', { suiteName: 'custom suite name', classname: 'custom-classname' }]
]
},
})
JSON 報告器
以 JSON 格式輸出測試結果報告。可以使用 outputFile
設定選項將其列印到終端機或寫入檔案。
npx vitest --reporter=json
export default defineConfig({
test: {
reporters: ['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 檔案。
npx vitest --reporter=html
export default defineConfig({
test: {
reporters: ['html']
},
})
提示
此報告器需要安裝 @vitest/ui
套件。
TAP 報告器
輸出遵循 測試任何事通訊協定 (TAP) 的報告。
npx vitest --reporter=tap
export default defineConfig({
test: {
reporters: ['tap']
},
})
TAP 報告範例
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
報告器一樣,測試結果會格式化為遵循 TAP 標準,但測試套件會格式化為平面清單,而不是巢狀層級。
npx vitest --reporter=tap-flat
export default defineConfig({
test: {
reporters: ['tap-flat']
},
})
TAP 平面報告範例
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
懸掛處理程序報告器
顯示懸掛處理程序清單(如果有任何處理程序阻止 Vitest 安全退出)。hanging-process
報告器本身不會顯示測試結果,但可用於與其他報告器結合使用,以在測試執行期間監控處理程序。使用此報告器可能會消耗大量資源,因此通常應保留在 Vitest 持續無法退出處理程序的情況下用於除錯目的。
npx vitest --reporter=hanging-process
export default defineConfig({
test: {
reporters: ['hanging-process']
},
})
Github Actions 報告器 1.3.0+
輸出 工作流程命令,以提供測試失敗的註解。當 process.env.GITHUB_ACTIONS === 'true'
時,此報告器會自動啟用 default
報告器。
如果您設定非預設報告器,則需要明確加入 github-actions
。
export default defineConfig({
test: {
reporters: process.env.GITHUB_ACTIONS ? ['dot', 'github-actions'] : ['dot'],
},
})
自訂報告器
你可以透過在 reporters 選項中指定套件名稱,來使用從 NPM 安裝的第三方自訂報告器
npx vitest --reporter=some-published-vitest-reporter
export default defineConfig({
test: {
reporters: ['some-published-vitest-reporter']
},
})
此外,你可以定義你自己的 自訂報告器,並透過指定其檔案路徑來使用它們
npx vitest --reporter=./path/to/reporter.ts
自訂報告器應實作 報告器介面。