Skip to content
Vitest 3
Main Navigation 指南 & API配置瀏覽器模式進階 API
3.2.0
2.1.9
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

API

Node API

進階 API

Vitest API

TestProject

TestSpecification

Test Task API

TestCase

TestSuite

TestModule

TestCollection

插件 API

執行器 API

報告器 API

任務中繼資料

指南

執行測試

擴展報告器

自訂 Pool

配置參考

測試 API 參考

本頁導覽

TestCase ​

TestCase 類別代表單一測試。此類別僅在主執行緒中可用。若您正在處理執行時任務,請參閱「執行器 API」。

TestCase 實例總是具有 type 屬性,其值為 test。您可以使用它來區分不同的任務類型:

ts
if (task.type === 'test') {
  task; // TestCase
}

project ​

此屬性參照測試所屬的 TestProject。

module ​

此屬性直接參照定義測試的 TestModule。

name ​

此屬性為傳遞給 test 函式的測試名稱。

ts
import { test } from 'vitest';

// [!code word:'the validation works correctly']
test('the validation works correctly', () => {
  // ...
});

fullName ​

此屬性為測試的完整名稱,包含所有父測試套件,並以 > 符號分隔。例如,此測試的完整名稱為「the validation logic > the validation works correctly」:

ts
import { describe, test } from 'vitest';

// [!code word:'the validation works correctly']
// [!code word:'the validation logic']
describe('the validation logic', () => {
  test('the validation works correctly', () => {
    // ...
  });
});

id ​

此屬性為測試的唯一識別符。此 ID 具有確定性,且在多次執行中對於相同的測試將保持不變。此 ID 基於 專案 (project) 名稱、模組 ID 和測試順序。

ID 格式如下:

1223128da3_0_0
^^^^^^^^^^ 檔案雜湊值
           ^ 測試套件索引
             ^ 測試索引

TIP

您可以使用 vitest/node 中的 generateFileHash 函式生成檔案雜湊值,此函式自 Vitest 3 起可用:

ts
import { generateFileHash } from 'vitest/node';

const hash = generateFileHash(
  '/file/path.js', // 相對路徑
  undefined // 專案名稱,若未設定則為 `undefined`
);

DANGER

請勿嘗試解析此 ID。它開頭可能帶有負號:-1223128da3_0_0_0。

location ​

此屬性表示測試在模組中定義的位置。僅當在設定檔中啟用 includeTaskLocation 時才會收集位置資訊。請注意,若使用 --reporter=html、--ui 或 --browser 旗標,此選項會自動啟用。

此測試的位置將等於 { line: 3, column: 1 }:

ts
import { test } from 'vitest'

test('the validation works correctly', () => {
  // ...
})
1
2
3
4
5

parent ​

此屬性為父 測試套件 (suite)。若測試直接在 模組 (module) 內部呼叫,則父級將是模組本身。

options ​

ts
interface TaskOptions {
  readonly each: boolean | undefined;
  readonly fails: boolean | undefined;
  readonly concurrent: boolean | undefined;
  readonly shuffle: boolean | undefined;
  readonly retry: number | undefined;
  readonly repeats: number | undefined;
  readonly mode: 'run' | 'only' | 'skip' | 'todo';
}

此屬性為收集此測試時所使用的選項。

ok ​

ts
function ok(): boolean;

此函式檢查測試是否未導致測試套件失敗。若測試尚未完成或已跳過,則會回傳 true。

meta ​

ts
function meta(): TaskMeta;

此函式回傳在測試執行期間附加到測試的自訂中繼資料 (metadata)。中繼資料可以透過在測試執行期間將屬性指派給 ctx.task.meta 物件來附加:

ts
import { test } from 'vitest';

test('the validation works correctly', ({ task }) => {
  // ...

  task.meta.decorated = false;
});

若測試尚未完成執行,中繼資料將是空物件。

result ​

ts
function result(): TestResult;

此函式回傳測試結果。若測試尚未完成或剛被收集,則為 TestResultPending:

ts
export interface TestResultPending {
  /**
   * 測試已收集,但尚未執行完成。
   */
  readonly state: 'pending';
  /**
   * 待處理的測試沒有任何錯誤。
   */
  readonly errors: undefined;
}

若測試被跳過,回傳值將是 TestResultSkipped:

ts
interface TestResultSkipped {
  /**
   * 測試已透過 `skip` 或 `todo` 旗標跳過。
   * 您可以在 `options.mode` 選項中查看使用了哪種模式。
   */
  readonly state: 'skipped';
  /**
   * 跳過的測試沒有錯誤。
   */
  readonly errors: undefined;
  /**
   * 傳遞給 `ctx.skip(note)` 的自訂註記。
   */
  readonly note: string | undefined;
}

TIP

若測試因為另一個測試具有 only 旗標而被跳過,則 options.mode 將等於 skip。

若測試失敗,回傳值將是 TestResultFailed:

ts
interface TestResultFailed {
  /**
   * 測試執行失敗。
   */
  readonly state: 'failed';
  /**
   * 在測試執行期間拋出的錯誤。
   */
  readonly errors: ReadonlyArray<TestError>;
}

若測試通過,回傳值將是 TestResultPassed:

ts
interface TestResultPassed {
  /**
   * 測試成功通過。
   */
  readonly state: 'passed';
  /**
   * 在測試執行期間拋出的錯誤。
   */
  readonly errors: ReadonlyArray<TestError> | undefined;
}

WARNING

請注意,狀態為 passed 的測試仍然可能帶有錯誤 - 這可能發生在至少觸發一次 retry 的情況下。

diagnostic ​

ts
function diagnostic(): TestDiagnostic | undefined;

此函式回傳關於測試的有用資訊,例如持續時間、記憶體用量等:

ts
interface TestDiagnostic {
  /**
   * 若測試的執行時間超過 `slowTestThreshold`。
   */
  readonly slow: boolean;
  /**
   * 測試所使用的記憶體量(位元組)。
   * 此值僅在測試以 `logHeapUsage` 旗標執行時可用。
   */
  readonly heap: number | undefined;
  /**
   * 執行測試所需時間(毫秒)。
   */
  readonly duration: number;
  /**
   * 測試開始時間(毫秒)。
   */
  readonly startTime: number;
  /**
   * 測試重試次數。
   */
  readonly retryCount: number;
  /**
   * 測試重複次數,由 `repeats` 選項設定。
   * 若測試在重複期間失敗且未設定 `retry`,則此值可能會較低。
   */
  readonly repeatCount: number;
  /**
   * 若測試在第二次重試時通過。
   */
  readonly flaky: boolean;
}

INFO

若測試尚未排定執行,diagnostic() 將回傳 undefined。

Pager
上一頁TestSpecification
下一頁TestSuite

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team

https://vitest.dev/advanced/api/test-case

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team