Skip to content

Commit

Permalink
Add more stats to specCoverageReport
Browse files Browse the repository at this point in the history
  • Loading branch information
VeskeR committed Jul 4, 2024
1 parent 214e8b6 commit 366f764
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions scripts/specCoverageReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class SpecItemNode implements SpecItem {
return this.childNodes.length === 0;
}

public isFullyCovered(): boolean {
return this.getCoveragePercentage() === 1;
}

public isPartiallyCovered(): boolean {
return this.getCoveragePercentage() > 0 && this.getCoveragePercentage() < 1;
}

public getCoveragePercentage(): number {
// general rules for coverage:
// if the spec item is covered by the test with @spec tag - it contributes a weigth of 1 (100%).
Expand Down Expand Up @@ -165,6 +173,10 @@ class SpecItemNodesCollection {
return rootNodes;
}

public getNodesArray(): SpecItemNode[] {
return [...this.allNodesByKeyMap.values()];
}

public getSpecItemLinesView(): string[] {
return [...this.allNodesByKeyMap.values()].map((x) => x.toStringView());
}
Expand Down Expand Up @@ -402,6 +414,42 @@ function roundTo(num: number, digits: number): number {
return res;
}

function printOverallCoverageMetrics(collection: SpecItemNodesCollection): void {
const total = collection.allNodesByKeyMap.size;
const fullyCovered = collection.getNodesArray().filter((x) => x.isFullyCovered()).length;
const partiallyCovered = collection.getNodesArray().filter((x) => x.isPartiallyCovered()).length;
const anyTestCovered = fullyCovered + partiallyCovered;

console.log(
`Out of ${total} total spec items` +
` ${fullyCovered} (${roundTo((fullyCovered / total) * 100, 2)}%) are fully covered by tests,` +
` and ${partiallyCovered} (${roundTo((partiallyCovered / total) * 100, 2)}%) are partially covered by tests.` +
` ${anyTestCovered} (${roundTo((anyTestCovered / total) * 100, 2)}%) spec items in total` +
` are tested in some way by ably-js tests.`,
);

const leafNodes = collection.getNodesArray().filter((x) => x.isLeaf());
const totalLeaves = leafNodes.length;
const fullyCoveredLeaves = leafNodes.filter((x) => x.isFullyCovered()).length;
const partiallyCoveredLeaves = leafNodes.filter((x) => x.isPartiallyCovered()).length;
const anyTestCoveredLeaves = fullyCovered + partiallyCovered;

console.log(
`Out of ${totalLeaves} leaf spec items` +
` ${fullyCoveredLeaves} (${roundTo((fullyCoveredLeaves / totalLeaves) * 100, 2)}%) are fully covered by tests,` +
` and ${partiallyCoveredLeaves} (${roundTo((partiallyCoveredLeaves / totalLeaves) * 100, 2)}%)` +
` are partially covered by tests.` +
` ${anyTestCoveredLeaves} (${roundTo((anyTestCoveredLeaves / totalLeaves) * 100, 2)}%) leaf spec items in total` +
` are tested in some way by ably-js tests.`,
);
}

function printCoverageTableForAllSpecItems(collection: SpecItemNodesCollection): void {
const table = specItemsCollectionToTable(collection);
console.log(`Coverage table for all spec items:`);
console.log(table.toString());
}

(async function main() {
try {
const specTextile = await loadSpecTextile();
Expand All @@ -415,9 +463,8 @@ function roundTo(num: number, digits: number): number {
applySpecCoveragesToSpecItemsCollection(collection, specCoverages);
}

const table = specItemsCollectionToTable(collection);
console.log(`Overall spec items coverage:`);
console.log(table.toString());
printOverallCoverageMetrics(collection);
printCoverageTableForAllSpecItems(collection);
} catch (error) {
console.error('Spec coverage report failed with error:', error);
}
Expand Down

0 comments on commit 366f764

Please sign in to comment.