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>;
僅過濾此集合中的測試套件。