Skip to content

Commit

Permalink
fix: test coverage lens shows "NaN%"
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeneos committed Jan 25, 2024
1 parent af4be29 commit 67e229d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
18 changes: 14 additions & 4 deletions packages/salesforce/src/salesforceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,20 @@ export class SalesforceService implements SalesforceConnectionProvider {
cache: false,
}
);
const results = new Map<string, ApexTestCoverage>(records.map(record => [
record.ApexClassOrTrigger.Name.toLowerCase(),
record.Coverage
]));

// Map the results to to class name and remove
// any records that do not have any coverage details
const results = new Map<string, ApexTestCoverage>(
records
.filter(record =>
record.Coverage.coveredLines?.length &&
record.Coverage.uncoveredLines?.length
)
.map(record => [
record.ApexClassOrTrigger.Name.toLowerCase(),
record.Coverage as ApexTestCoverage
])
);

if (!Array.isArray(apexClassName)) {
return results.get(apexClassName.toLowerCase())!;
Expand Down
17 changes: 9 additions & 8 deletions packages/vscode-extension/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,26 +350,27 @@ vlocode.omniScript.activate:
vlocode.openMetaXml:
title: 'Salesforce: Open -meta.xml'
icon: resources/{type}/meta-xml.svg
when:
- resourceScheme == file && resourcePath =~ /\.cls$/
- resourceScheme == file && resourcePath =~ /\.trigger$/
menus:
- menu: editor/title/run
when:
- resourceScheme == file && resourceExtname =~ /\.cls$/
- resourceScheme == file && resourceExtname =~ /\.trigger$/
- menu: commandPalette
vlocode.openSourceFile:
title: 'Salesforce: Open Source'
icon: $(code)
when:
- resourceScheme == file && resourcePath =~ /\.cls-meta\.xml$/
- resourceScheme == file && resourcePath =~ /\.trigger-meta\.xml$/
menus:
- menu: editor/title/run
when:
- resourceScheme == file && resourcePath =~ /\.cls-meta\.xml$/
- resourceScheme == file && resourcePath =~ /\.trigger-meta\.xml$/
- menu: commandPalette
vlocode.apex.toggleCoverage:
title: 'APEX: Toggle Test Coverage'
title: 'APEX: Highlight Unit Test Coverage'
icon: $(test-view-icon)
group: v_apex
when:
- resourceScheme == file && resourcePath =~ /\.cls$/
- resourceExtname == .cls
menus:
- menu: commandPalette
- menu: editor/context
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ export class TestCoverageLensProvider implements vscode.CodeLensProvider<TestCov
class TestCoverageCodeLens extends vscode.CodeLens {
constructor(range: vscode.Range, public readonly className: string, coverageDetails: ApexTestCoverage) {
super(range, {
title: `Test Coverage (${TestCoverageCodeLens.getCoveragePercentage(coverageDetails)}%)`,
title: `${TestCoverageCodeLens.getCoveragePercentage(coverageDetails)}% Test Coverage`,
command: VlocodeCommand.apexToggleCoverage,
arguments: [ coverageDetails ]
});
}

/**
* Get the coverage percentage for the specified coverage details in the format of 0.0 - 100.0.
* 100.0 means all lines are covered and 0.0 means no lines are covered.
* @param coverageDetails
* @returns
*/
static getCoveragePercentage(coverageDetails: ApexTestCoverage) {
return Math.round(
coverageDetails.coveredLines.length /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ export default class ToggleApexTestCoverage extends CommandBase {

const coverageDetails = await this.getCoverage(apexClassName);
if (!coverageDetails || !coverageDetails.coveredLines.length && !coverageDetails.uncoveredLines.length) {
void vscode.window.showWarningMessage(`No test coverage collected for ${apexClassName}.`);
void vscode.window.showWarningMessage(`No test coverage data available for ${apexClassName}.`);
return;
}

const maxLine = Math.max(...coverageDetails.coveredLines, ...coverageDetails.uncoveredLines) - 1;
if (maxLine > document.lineCount) {
void vscode.window.showWarningMessage(`Coverage data for ${apexClassName} is out of sync with the current file.`);
void vscode.window.showWarningMessage(`Coverage data for ${apexClassName} is out of sync.`);
return;
}

Expand Down

0 comments on commit 67e229d

Please sign in to comment.