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 參考

本頁導覽

TestCollection ​

TestCollection 代表了套件或模組中頂層的測試套件與測試案例的集合。它亦提供了用於迭代自身的方法。

INFO

大多數方法會返回一個迭代器(Iterator)而非陣列,以便在您不需要集合中所有項目時獲得更佳效能。如果您偏好使用陣列,您可以展開迭代器:[...children.allSuites()]。

另請注意,集合本身即為一個迭代器:

ts
for (const child of module.children) {
  console.log(child.type, child.name);
}

size ​

集合中測試案例與測試套件的數量。

WARNING

此數字僅包含頂層的測試案例與測試套件,不包含巢狀的測試套件與測試案例。

at ​

ts
function at(index: number): TestCase | TestSuite | undefined;

返回特定索引處的測試案例或測試套件。此方法接受負數索引。

array ​

ts
function array(): (TestCase | TestSuite)[];

返回與此集合內容相同的陣列。如果您想使用 Array 的方法(例如 map 和 filter,這些方法不被 TaskCollection 實作支援),這會很有用。

allSuites ​

ts
function allSuites(): Generator<TestSuite, undefined, void>;

過濾此集合及其所有子集合中的測試套件。

ts
for (const suite of module.children.allSuites()) {
  if (suite.errors().length) {
    console.log('failed to collect', suite.errors());
  }
}

allTests ​

ts
function allTests(state?: TestState): Generator<TestCase, undefined, void>;

過濾此集合及其所有子集合中的測試案例。

ts
for (const test of module.children.allTests()) {
  if (test.result().state === 'pending') {
    console.log('test', test.fullName, 'did not finish');
  }
}

您可以傳入 state 值來依狀態過濾測試案例。

tests ​

ts
function tests(state?: TestState): Generator<TestCase, undefined, void>;

僅過濾此集合中的測試案例。您可以傳入 state 值來依狀態過濾測試案例。

suites ​

ts
function suites(): Generator<TestSuite, undefined, void>;

僅過濾此集合中的測試套件。

Pager
上一頁TestModule
下一頁插件 API

以 MIT 授權條款 發布。

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

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

以 MIT 授權條款 發布。

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