Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const messages = {
testNameColHeader: 'TEST NAME',
testSetupMethodNameColHeader: 'TEST SETUP METHOD NAME',
outcomeColHeader: 'OUTCOME',
categoryColHeader: 'CATEGORY',
msgColHeader: 'MESSAGE',
runtimeColHeader: 'RUNTIME (MS)',
setupTimeColHeader: 'SETUP TIME',
Expand Down
85 changes: 67 additions & 18 deletions src/reporters/humanReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ export class HumanReporter {
public format(
testResult: TestResult,
detailedCoverage: boolean,
concise: boolean = false
concise: boolean = false,
showCategory: boolean = false
): string {
HeapMonitor.getInstance().checkHeapSize('HumanReporter.format');
try {
return [
...(!testResult.codecoverage || !detailedCoverage
? [this.formatTestResults(testResult.tests, concise)]
? [this.formatTestResults(testResult.tests, concise, showCategory)]
: []),
...(testResult.codecoverage && detailedCoverage
? [this.formatDetailedCov(testResult, concise)]
? [this.formatDetailedCov(testResult, concise, showCategory)]
: []),
...(testResult.codecoverage && !concise
? [this.formatCodeCov(testResult.codecoverage)]
Expand Down Expand Up @@ -118,7 +119,8 @@ export class HumanReporter {
@elapsedTime()
private formatTestResults(
tests: ApexTestResultData[],
concise: boolean
concise: boolean,
showCategory: boolean
): string {
const testRowArray: Row[] = tests
.filter(
Expand All @@ -129,26 +131,38 @@ export class HumanReporter {
)
.map((elem) => ({
name: elem.fullName,
...(showCategory && { category: elem.category }),
outcome: elem.outcome,
msg: buildMsg(elem),
runtime:
elem.outcome !== ApexTestResultOutcome.Fail ? `${elem.runTime}` : ''
}));

if (testRowArray.length > 0) {
return new Table().createTable(
testRowArray,
[
{
key: 'name',
label: nls.localize('testNameColHeader')
},
{ key: 'outcome', label: nls.localize('outcomeColHeader') },
{ key: 'msg', label: nls.localize('msgColHeader') },
{ key: 'runtime', label: nls.localize('runtimeColHeader') }
],
nls.localize('testResultsHeader')
);
if (showCategory) {
return new Table().createTable(
testRowArray,
[
{ key: 'name', label: nls.localize('testNameColHeader') },
{ key: 'category', label: nls.localize('categoryColHeader') },
{ key: 'outcome', label: nls.localize('outcomeColHeader') },
{ key: 'msg', label: nls.localize('msgColHeader') },
{ key: 'runtime', label: nls.localize('runtimeColHeader') }
],
nls.localize('testResultsHeader')
);
} else {
return new Table().createTable(
testRowArray,
[
{ key: 'name', label: nls.localize('testNameColHeader') },
{ key: 'outcome', label: nls.localize('outcomeColHeader') },
{ key: 'msg', label: nls.localize('msgColHeader') },
{ key: 'runtime', label: nls.localize('runtimeColHeader') }
],
nls.localize('testResultsHeader')
);
}
}
return '';
}
Expand Down Expand Up @@ -179,7 +193,11 @@ export class HumanReporter {
}

@elapsedTime()
private formatDetailedCov(testResult: TestResult, concise: boolean): string {
private formatDetailedCov(
testResult: TestResult,
concise: boolean,
showCategory: boolean
): string {
const testRowArray: Row[] = testResult.tests
.filter(
(elem: ApexTestResultData) =>
Expand All @@ -190,6 +208,7 @@ export class HumanReporter {
.flatMap((elem) => {
const base = {
name: elem.fullName,
...(showCategory && { category: elem.category }),
outcome: elem.outcome,
msg: buildMsg(elem),
runtime: `${elem.runTime}`
Expand All @@ -211,6 +230,36 @@ export class HumanReporter {
});

if (testRowArray.length > 0) {
if (showCategory) {
return new Table().createTable(
testRowArray,
[
{
key: 'name',
label: nls.localize('testNameColHeader')
},
{
key: 'coveredClassName',
label: nls.localize('classTestedHeader')
},
{
key: 'category',
label: nls.localize('categoryColHeader')
},
{
key: 'outcome',
label: nls.localize('outcomeColHeader')
},
{
key: 'coveredClassPercentage',
label: nls.localize('percentColHeader')
},
{ key: 'msg', label: nls.localize('msgColHeader') },
{ key: 'runtime', label: nls.localize('runtimeColHeader') }
],
nls.localize('detailedCodeCovHeader', [testResult.summary.testRunId])
);
}
return new Table().createTable(
testRowArray,
[
Expand Down
137 changes: 110 additions & 27 deletions src/tests/asyncTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
TestResultRaw,
TestRunIdResult,
FlowTestResult,
ApexTestResultRecord
ApexTestResultRecord,
TestCategory
} from './types';
import {
calculatePercentage,
Expand Down Expand Up @@ -484,32 +485,21 @@ export class AsyncTests {
HeapMonitor.getInstance().checkHeapSize('asyncTests.getAsyncTestResults');
const hasIsTestSetupField = await this.supportsTestSetupFeature();
try {
const resultIds = testQueueResult.records.map((record) => record.Id);
const isFlowRunTest = await this.isJobIdForFlowTestRun(resultIds[0]);
const testResultQuery = isFlowRunTest
? `SELECT Id, ApexTestQueueItemId, Result, TestStartDateTime,TestEndDateTime, FlowTest.DeveloperName, FlowDefinition.DeveloperName, FlowDefinition.NamespacePrefix FROM FlowTestResult WHERE ApexTestQueueItemId IN (%s)`
: hasIsTestSetupField
? `SELECT Id, QueueItemId, StackTrace, Message, RunTime, TestTimestamp, AsyncApexJobId, MethodName, Outcome, ApexLogId, IsTestSetup, ApexClass.Id, ApexClass.Name, ApexClass.NamespacePrefix FROM ApexTestResult WHERE QueueItemId IN (%s)`
: `SELECT Id, QueueItemId, StackTrace, Message, RunTime, TestTimestamp, AsyncApexJobId, MethodName, Outcome, ApexLogId, ApexClass.Id, ApexClass.Name, ApexClass.NamespacePrefix FROM ApexTestResult WHERE QueueItemId IN (%s)`;

// iterate thru ids, create query with id, & compare query length to char limit
const queries: string[] = [];
for (let i = 0; i < resultIds.length; i += QUERY_RECORD_LIMIT) {
const recordSet: string[] = resultIds
.slice(i, i + QUERY_RECORD_LIMIT)
.map((id) => `'${id}'`);
const query: string = util.format(testResultQuery, recordSet.join(','));
queries.push(query);
}
const connection = await this.defineApiVersion();
const queryPromises = queries.map(async (query) =>
queryAll(connection, query, true)
const { apexTestIds, flowTestIds } = this.mapTestResultsByCategory(
testQueueResult.records
);
const testResults = await Promise.all(queryPromises);
if (isFlowRunTest) {
return this.convertFlowTestResult(testResults as FlowTestResult[]);

const allTestResults: ApexTestResult[] = [];
if (apexTestIds.length > 0) {
allTestResults.push(
...(await this.getApexTestResults(apexTestIds, hasIsTestSetupField))
);
}
if (flowTestIds.length > 0) {
allTestResults.push(...(await this.getFlowTestResults(flowTestIds)));
}
return testResults as ApexTestResult[];

return allTestResults as ApexTestResult[];
} finally {
HeapMonitor.getInstance().checkHeapSize('asyncTests.getAsyncTestResults');
}
Expand Down Expand Up @@ -553,7 +543,8 @@ export class AsyncTests {
return {
done: flowtestResult.done,
totalSize: tmpRecords.length,
records: tmpRecords
records: tmpRecords,
category: TestCategory.Flow
};
});
}
Expand Down Expand Up @@ -622,7 +613,8 @@ export class AsyncTests {
runTime: item.RunTime ?? 0,
testTimestamp: item.TestTimestamp, // TODO: convert timestamp
fullName: `${item.ApexClass.FullName}.${item.MethodName}`,
...(diagnostic ? { diagnostic } : {})
...(diagnostic ? { diagnostic } : {}),
category: result.category
});
});
}
Expand Down Expand Up @@ -767,4 +759,95 @@ export class AsyncTests {
);
}
}

/**
* Maps test results by category (Apex vs Flow tests)
*/
public mapTestResultsByCategory(records: ApexTestQueueItemRecord[]): {
apexTestIds: string[];
flowTestIds: string[];
} {
if (!records?.length) {
return { apexTestIds: [], flowTestIds: [] };
}
return {
apexTestIds: records
.filter((r) => r.ApexClassId !== null)
.map((r) => r.Id),
flowTestIds: records
.filter((r) => r.ApexClassId === null)
.map((r) => r.Id)
};
}

public async getFlowTestResults(
recordIds: string[]
): Promise<ApexTestResult[]> {
const queryTemplate = `SELECT Id, ApexTestQueueItemId, Result, TestStartDateTime,TestEndDateTime, FlowTest.DeveloperName, FlowDefinition.DeveloperName, FlowDefinition.NamespacePrefix FROM FlowTestResult WHERE ApexTestQueueItemId IN (%s)`;
const queries = this.buildChunkedQueries(queryTemplate, recordIds);

const connection = await this.defineApiVersion();
const queryPromises = queries.map(async (query) =>
queryAll(connection, query, true)
);

const testResults = await Promise.all(queryPromises);
return this.convertFlowTestResult(testResults as FlowTestResult[]);
}

public async getApexTestResults(
recordIds: string[],
isTestSetup: boolean
): Promise<ApexTestResult[]> {
const queryTemplate = isTestSetup
? `SELECT Id, QueueItemId, StackTrace, Message, RunTime, TestTimestamp, AsyncApexJobId, MethodName, Outcome, ApexLogId, IsTestSetup, ApexClass.Id, ApexClass.Name, ApexClass.NamespacePrefix FROM ApexTestResult WHERE QueueItemId IN (%s)`
: `SELECT Id, QueueItemId, StackTrace, Message, RunTime, TestTimestamp, AsyncApexJobId, MethodName, Outcome, ApexLogId, ApexClass.Id, ApexClass.Name, ApexClass.NamespacePrefix FROM ApexTestResult WHERE QueueItemId IN (%s)`;
const queries = this.buildChunkedQueries(queryTemplate, recordIds);

const connection = await this.defineApiVersion();
const queryPromises = queries.map(async (query) =>
queryAll(connection, query, true)
);

const testResults = await Promise.all(queryPromises);
return testResults.map(
(result) =>
({
...result,
category: TestCategory.Apex
}) as ApexTestResult
);
}

/**
* Splits record IDs into multiple queries to respect Salesforce query limits
* @param queryTemplate SOQL query template with %s placeholder for IDs
* @param recordIds Array of record IDs to include in queries
* @returns Array of complete SOQL queries ready to execute
*/
public buildChunkedQueries(
queryTemplate: string,
recordIds: string[]
): string[] {
if (!queryTemplate?.includes('%s')) {
throw new Error(
'Query template must contain %s placeholder for record IDs'
);
}

if (!recordIds?.length) {
return [];
}

const queries: string[] = [];

for (let i = 0; i < recordIds.length; i += QUERY_RECORD_LIMIT) {
const chunk = recordIds.slice(i, i + QUERY_RECORD_LIMIT);
const quotedIds = chunk.map((id) => `'${id}'`).join(',');
const query = util.format(queryTemplate, quotedIds);
queries.push(query);
}

return queries;
}
}
1 change: 1 addition & 0 deletions src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
OutputDirConfig,
ResultFormat,
SyncTestConfiguration,
TestCategory,
TestItem,
TestLevel,
TestResult,
Expand Down
4 changes: 3 additions & 1 deletion src/tests/syncTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import {
calculateCodeCoverage,
calculatePercentage,
computeTestCategory,
transformTestResult
} from './utils';
import type { HttpRequest } from '@jsforce/jsforce-node';
Expand Down Expand Up @@ -216,7 +217,8 @@ export class SyncTests {
},
runTime: item.time ?? 0,
testTimestamp: '',
fullName: `${nms}${item.name}.${item.methodName}`
fullName: `${nms}${item.name}.${item.methodName}`,
category: computeTestCategory(item.namespace)
};

if (outcome === ApexTestResultOutcome.Fail) {
Expand Down
Loading
Loading