Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

fix(apex-tests): clear code coverage and existing test result before test execution #871

Merged
merged 3 commits into from
Mar 1, 2022
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
76 changes: 76 additions & 0 deletions packages/core/src/sfpcommands/apextest/ClearCodeCoverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Connection, Org } from "@salesforce/core";
import SFPLogger, { Logger, LoggerLevel } from "../../logger/SFPLogger";
import QueryHelper from "../../queryHelper/QueryHelper";
import { chunkArray } from "../../utils/ChunkArray";

const CODECOV_AGGREGATE_QUERY = `SELECT Id FROM ApexCodeCoverageAggregate`;
const APEX_TEST_RESULT_QUERY = `SELECT Id FROM ApexTestResult`;

export default class ClearTestResults {
private conn: Connection;

public constructor(private org: Org, private logger: Logger) {}
/**
* Clear coverage and test results
*/
public async clear() {
this.conn = this.org.getConnection();

SFPLogger.log(
`Clearing Existing Coverage and Test Results`,
LoggerLevel.INFO,
this.logger
);


SFPLogger.log(
`Clearing Coverage Results`,
LoggerLevel.DEBUG,
this.logger
);
let codeCoverageAggregate = await QueryHelper.query(
CODECOV_AGGREGATE_QUERY,
this.conn,
true
);
await this.deleteRecords("ApexCodeCoverageAggregate", codeCoverageAggregate);
SFPLogger.log(`Cleared Coverage Results`, LoggerLevel.DEBUG, this.logger);

SFPLogger.log(`Clearing Test Results`, LoggerLevel.DEBUG, this.logger);
let testResults = await QueryHelper.query(
APEX_TEST_RESULT_QUERY,
this.conn,
true
);
await this.deleteRecords("ApexTestResult", testResults);
SFPLogger.log(`Cleared Test Results`, LoggerLevel.DEBUG, this.logger);

SFPLogger.log(
`Clearing Existing Coverage and Test Results`,
LoggerLevel.INFO,
this.logger
);
}

private async deleteRecords(objectType: string, records: any[]) {
if (records && records.length > 0) {
let idsList: string[] = records.map((elem) => elem.Id);
let errors = [];
for (let idsToDelete of chunkArray(2000, idsList)) {
const deleteResults: any = await this.conn.tooling.destroy(
objectType,
idsToDelete
);
deleteResults.forEach((elem) => {
if (!elem.success) {
errors = errors.concat(elem.errors);
}
});
}

if (errors.length > 0) {
throw new Error(JSON.stringify(errors));
}
}
}
}
16 changes: 15 additions & 1 deletion packages/core/src/sfpcommands/apextest/TriggerApexTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SFPLogger, { LoggerLevel } from "../../logger/SFPLogger";
import { RunAllTestsInPackageOptions } from "./ExtendedTestOptions";
import SFPStatsSender from "../../stats/SFPStatsSender";
import { Connection, Org } from "@salesforce/core";
import ClearCodeCoverage from "./ClearCodeCoverage";

export default class TriggerApexTests {
private conn: Connection;
Expand All @@ -30,7 +31,20 @@ export default class TriggerApexTests {
}> {
this.cleanupStaleTestArtifacts();

this.conn = (await Org.create({aliasOrUsername: this.target_org})).getConnection();
let org = await Org.create({aliasOrUsername: this.target_org});
this.conn = org.getConnection();


//Clear Code Coverage before triggering tests
try
{
let clearCodeCoverage = new ClearCodeCoverage(org,this.fileLogger);
await clearCodeCoverage.clear();
}
catch(error)
{
SFPLogger.log(`Ignoring error in clearing code coverage attributed to ${error}.`,LoggerLevel.DEBUG,this.fileLogger);
}

let startTime = Date.now();
let testExecutionResult: boolean = false;
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/utils/ChunkArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function chunkArray(perChunk: number, inputArray: any[]): Array<any> {
let chunks = [],
i = 0,
n = inputArray.length;

while (i < n) {
chunks.push(inputArray.slice(i, (i += perChunk)));
}

return chunks;
}
18 changes: 18 additions & 0 deletions packages/core/tests/utils/ChunkArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from "@jest/globals";
import { chunkArray } from "../../src/utils/ChunkArray";

describe("Given an input array", () => {
const input = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

it("should chunk for even chunk size", () => {
const result = chunkArray(2, input);
expect(result.length).toBe(5);
expect(result).toEqual([[1,2], [3,4], [5,6], [7,8], [9,10]]);
});

it("should chunk for odd chunk size", () => {
const result = chunkArray(3, input);
expect(result.length).toBe(4);
expect(result).toEqual([[1,2,3], [4,5,6], [7,8,9], [10]]);
});
});