All files / infrastructure/scripts/src/coverage-report/domain merge.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 6/6
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                    1x 4x 7x 3x 4x 2x           1x 4x 4x 1x           1x 2x   2x 2x 2x 2x   1x    
import { isAbsolute } from "node:path";
import type { CoverageMap, CoverageMapData } from "istanbul-lib-coverage";
import libCoverage from "istanbul-lib-coverage";
import { ExpectedError } from "../../shared/errors.ts";
 
interface LoadedReport {
    name: string;
    data: CoverageMapData | undefined;
}
 
export const assertComplete = (reports: LoadedReport[]): void => {
    const missing = reports
        .filter((report) => report.data === undefined)
        .map((report) => report.name);
    if (missing.length > 0) {
        throw new ExpectedError(
            `No coverage-final.json for: ${missing.join(", ")}. Run \`nx run-many -t test:coverage\` first, not \`affected\`.`,
        );
    }
};
 
export const assertAbsolutePaths = (name: string, data: CoverageMapData): void => {
    const relativePaths = Object.keys(data).filter((path) => !isAbsolute(path));
    if (relativePaths.length > 0) {
        throw new ExpectedError(
            `${name}'s coverage-final.json has relative paths, which would collide once merged: ${relativePaths.join(", ")}`,
        );
    }
};
 
export const mergeReports = (reports: LoadedReport[]): CoverageMap => {
    assertComplete(reports);
 
    const map = libCoverage.createCoverageMap({});
    for (const { name, data } of reports) {
        assertAbsolutePaths(name, data as CoverageMapData);
        map.merge(data as CoverageMapData);
    }
    return map;
};