From cdc8f6e615f4cfa9c5accaf6c420bc2333258c78 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Wed, 22 Mar 2023 18:45:19 -0500 Subject: [PATCH] refactor: typesafe testCov --- src/utils/coverage.ts | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/utils/coverage.ts b/src/utils/coverage.ts index cb61447f..a18541d9 100644 --- a/src/utils/coverage.ts +++ b/src/utils/coverage.ts @@ -19,26 +19,22 @@ import { Successes, Failures, CodeCoverage } from '@salesforce/source-deploy-ret import { ensureArray } from '@salesforce/kit'; import { StandardColors } from '@salesforce/sf-plugins-core'; -type SuccessOrFailure = Successes & Failures; - -export const mapTestResults = (testResults: Failures[] | Successes[]): ApexTestResultData[] => - testResults.map((successOrFailure) => { - const testResult = successOrFailure as SuccessOrFailure; - return { - apexClass: { fullName: testResult.name, id: testResult.id, name: testResult.name, namespacePrefix: '' }, - apexLogId: '', - asyncApexJobId: '', - fullName: testResult.name, - id: testResult.id, - message: testResult.message ?? '', - methodName: testResult.methodName, - outcome: !testResult.message ? ApexTestResultOutcome.Pass : ApexTestResultOutcome.Fail, - queueItemId: '', - runTime: parseInt(testResult.time, 10), - stackTrace: testResult.stackTrace || '', - testTimestamp: '', - }; - }); +export const mapTestResults = (testResults: T[]): ApexTestResultData[] => + testResults.map((testResult) => ({ + apexClass: { fullName: testResult.name, id: testResult.id, name: testResult.name, namespacePrefix: '' }, + apexLogId: '', + asyncApexJobId: '', + fullName: testResult.name, + id: testResult.id, + ...('message' in testResult && testResult.message + ? { message: testResult.message, outcome: ApexTestResultOutcome.Pass } + : { message: null, outcome: ApexTestResultOutcome.Fail }), + methodName: testResult.methodName, + queueItemId: '', + runTime: parseInt(testResult.time, 10), + stackTrace: 'stackTrace' in testResult ? testResult.stackTrace : null, + testTimestamp: '', + })); export const generateCoveredLines = (cov: CodeCoverage): [number[], number[]] => { const numCovered = parseInt(cov.numLocations, 10);