TestCollection
TestCollection
表示一个模块或套件中顶层的套件和测试的集合。它还提供了遍历自身内容的实用方法。
INFO
大多数方法返回迭代器而非数组,以便在您不需要集合中的每个项目时获得更好的性能。如果您更倾向于使用数组,可以展开迭代器:[...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>;
仅筛选此集合中顶层的套件。