Skip to content

Commit

Permalink
fix: fix generated code coverage result percent (#1175)
Browse files Browse the repository at this point in the history
* fix: fix generated code coverage result percent

* test: add UT
  • Loading branch information
WillieRuemmele authored Oct 2, 2024
1 parent fe0d16b commit d4cdac1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export const mapTestResults = <T extends Failures | Successes>(testResults: T[])
}));

export const generateCoveredLines = (cov: CodeCoverage): [number[], number[]] => {
const [lineCount, uncoveredLineCount] = getCoverageNumbers(cov);
const [lineCount] = getCoverageNumbers(cov);
const uncoveredLines = ensureArray(cov.locationsNotCovered).map((location) => parseInt(location.line, 10));
const minLineNumber = uncoveredLines.length ? Math.min(...uncoveredLines) : 1;
const lines = [...Array(lineCount + uncoveredLineCount).keys()].map((i) => i + minLineNumber);
const lines = [...Array(lineCount).keys()].map((i) => i + minLineNumber);
const coveredLines = lines.filter((line) => !uncoveredLines.includes(line));
return [uncoveredLines, coveredLines];
};
Expand Down
5 changes: 4 additions & 1 deletion test/utils/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { expect } from 'chai';
import { ApexTestResultOutcome } from '@salesforce/apex-node';
import { StandardColors } from '@salesforce/sf-plugins-core';
import { coverageOutput, getCoveragePct, mapTestResults } from '../../src/utils/coverage.js';
import { coverageOutput, generateCoveredLines, getCoveragePct, mapTestResults } from '../../src/utils/coverage.js';

// methods are mutating the object instead of returning new ones
function getSampleTestResult() {
Expand Down Expand Up @@ -373,6 +373,9 @@ describe('coverage utils', () => {
it('1 uncovered of 4', () => {
expect(getCoveragePct(getSampleTestResult().codeCoverage[0])).equal(75);
});
it('will generate covered lines correctly', () => {
expect(generateCoveredLines(getSampleTestResult().codeCoverage[0])).to.deep.equal([[12], [13, 14, 15]]);
});
it('rounds 3 uncovered out of 44 to the nearest integer', () => {
expect(getCoveragePct(getSampleTestResult().codeCoverage[1])).equal(93);
});
Expand Down

0 comments on commit d4cdac1

Please sign in to comment.