Skip to content

Commit

Permalink
feat: baselineBranchName param added
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbaser authored Jan 23, 2023
1 parent 98a79a2 commit 40faea0
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 22 deletions.
4 changes: 4 additions & 0 deletions lib/helpers/config.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("config.helper", () => {
const updatedConfig: Config = {
apiUrl: "apiUrlUpdated",
branchName: "branchNameUpdated",
baselineBranchName: "baselineBranchNameUpdated",
project: "projectUpdated",
apiKey: "apiKeyUpdated",
enableSoftAssert: false,
Expand Down Expand Up @@ -79,6 +80,7 @@ describe("config.helper", () => {
VRT_APIURL: "apiUrlTest",
VRT_CIBUILDID: "ciBuildIdTest",
VRT_BRANCHNAME: "branchNameTest",
VRT_BASELINEBRANCHNAME: "baselineBranchNameTest",
VRT_PROJECT: "projectTest",
VRT_APIKEY: "apiKeyTest",
VRT_ENABLESOFTASSERT: "false",
Expand All @@ -90,6 +92,7 @@ describe("config.helper", () => {
apiUrl: "apiUrlTest",
ciBuildId: "ciBuildIdTest",
branchName: "branchNameTest",
baselineBranchName: "baselineBranchNameTest",
project: "projectTest",
apiKey: "apiKeyTest",
enableSoftAssert: false,
Expand All @@ -101,6 +104,7 @@ describe("config.helper", () => {
VRT_APIURL: undefined,
VRT_CIBUILDID: undefined,
VRT_BRANCHNAME: undefined,
VRT_BASELINEBRANCHNAME: undefined,
VRT_PROJECT: undefined,
VRT_APIKEY: undefined,
VRT_ENABLESOFTASSERT: undefined,
Expand Down
6 changes: 6 additions & 0 deletions lib/helpers/config.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const readConfigFromFile = (config: Config): Config => {
if (fileConfig.branchName) {
config.branchName = fileConfig.branchName;
}
if (fileConfig.baselineBranchName) {
config.baselineBranchName = fileConfig.baselineBranchName;
}
if (fileConfig.project) {
config.project = fileConfig.project;
}
Expand All @@ -35,6 +38,9 @@ const readConfigFromEnv = (config: Config): Config => {
if (process.env["VRT_BRANCHNAME"]) {
config.branchName = process.env["VRT_BRANCHNAME"];
}
if (process.env["VRT_BASELINEBRANCHNAME"]) {
config.baselineBranchName = process.env["VRT_BASELINEBRANCHNAME"];
}
if (process.env["VRT_PROJECT"]) {
config.project = process.env["VRT_PROJECT"];
}
Expand Down
2 changes: 2 additions & 0 deletions lib/helpers/dto.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const multipartDtoToFormData = (dto: TestRunMultipartDto): FormData => {
data.append("buildId", dto.buildId);
data.append("projectId", dto.projectId);
data.append("branchName", dto.branchName);
dto.baselineBranchName && data.append("baselineBranchName", dto.baselineBranchName);
data.append("name", dto.name);
data.append("image", fs.createReadStream(dto.imagePath), {
knownLength: fs.statSync(dto.imagePath).size,
Expand All @@ -30,6 +31,7 @@ export const bufferDtoToFormData = (dto: TestRunBufferDto): FormData => {
data.append("buildId", dto.buildId);
data.append("projectId", dto.projectId);
data.append("branchName", dto.branchName);
dto.baselineBranchName && data.append("baselineBranchName", dto.baselineBranchName);
data.append("name", dto.name);
data.append("image", dto.imageBuffer, { filename: "image.png" });
dto.os && data.append("os", dto.os);
Expand Down
57 changes: 56 additions & 1 deletion lib/helpers/track.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { TestRunResponse, TestStatus } from "../types";
import { processTestRun, shouldStopRetry } from "./track.helper";
import {
processTestRun,
shouldStopRetry,
trackWithRetry,
} from "./track.helper";
import {
testRunOkResponse,
testRunUnresolvedResponse,
Expand Down Expand Up @@ -43,6 +47,57 @@ describe.each<[TestStatus.new | TestStatus.unresolved, string]>([
});
});

const unresolvedResponce: TestRunResponse = {
id: "someId",
imageName: "imageName",
diffName: "diffName",
baselineName: "baselineName",
diffPercent: 1.11,
diffTollerancePercent: 2.22,
pixelMisMatchCount: 3,
status: TestStatus.unresolved,
url: "url",
merge: true,
};

const okResponce: TestRunResponse = {
...unresolvedResponce,
status: TestStatus.ok,
};

it("should stop when diff not found", async () => {
// .Arrange
const trackMock = jest.fn().mockReturnValue(okResponce);

// .Act
await trackWithRetry(trackMock, 5);

// .Assert
expect(trackMock).toBeCalledTimes(1);
});

it("should stop on default retry limit", async () => {
// .Arrange
const trackMock = jest.fn().mockReturnValue(unresolvedResponce);

// .Act
await trackWithRetry(trackMock, undefined as unknown as number, true);

// .Assert
expect(trackMock).toBeCalledTimes(3);
});

it("should stop on custom retry limit", async () => {
// .Arrange
const trackMock = jest.fn().mockReturnValue(unresolvedResponce);

// .Act
await trackWithRetry(trackMock, 5, true);

// .Assert
expect(trackMock).toBeCalledTimes(6);
});

it.each<[TestRunResponse, boolean]>([
[testRunOkResponse, true],
[testRunUnresolvedResponse, false],
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/track.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const shouldStopRetry = (result: TestRunResponse) =>

export const trackWithRetry = async (
trackFn: () => Promise<TestRunResponse>,
retryLimit: number,
retryLimit: number = 2,
enableSoftAssert?: boolean
): Promise<TestRunResponse> => {
const result = await trackFn();
Expand Down
6 changes: 5 additions & 1 deletion lib/helpers/type.helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { TestRunBase64, TestRunBuffer } from "../types";
import { TestRunBase64, TestRunBuffer, TestRunMultipart } from "../types";

export function instanceOfTestRunBase64(object: any): object is TestRunBase64 {
return "imageBase64" in object;
}

export function instanceOfTestRunBuffer(object: any): object is TestRunBuffer {
return "imageBuffer" in object;
}

export function instanceOfTestRunMultipart(object: any): object is TestRunMultipart {
return "imagePath" in object;
}
1 change: 1 addition & 0 deletions lib/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Config extends Record<string, any> {
apiUrl: string;
branchName: string;
baselineBranchName?: string;
project: string;
apiKey: string;
enableSoftAssert?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions lib/types/request/testRun.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface TestRunDto {

branchName: string;

baselineBranchName?: string

buildId: string;

projectId: string;
Expand Down
8 changes: 8 additions & 0 deletions lib/visualRegressionTracker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const axiosErrorEmptyResponse: AxiosError = {
const config: Config = {
apiUrl: "http://localhost:4200",
branchName: "develop",
baselineBranchName: "next-release",
project: "Default project",
apiKey: "CPKVK4JNK24NVNPNGVFQ853HXXEG",
enableSoftAssert: false,
Expand Down Expand Up @@ -176,6 +177,7 @@ describe("VisualRegressionTracker", () => {
const fileConfig: Config = {
apiUrl: "apiUrlFile",
branchName: "branchNameFile",
baselineBranchName: "baselineBranchNameFile",
project: "projectFile",
apiKey: "apiKeyFile",
enableSoftAssert: false,
Expand All @@ -184,6 +186,7 @@ describe("VisualRegressionTracker", () => {
const envConfig: Config = {
apiUrl: "apiUrlEnv",
branchName: "branchNameEnv",
baselineBranchName: "baselineBranchNameEnv",
project: "projectEnv",
apiKey: "apiKeyEnv",
enableSoftAssert: false,
Expand Down Expand Up @@ -282,6 +285,7 @@ describe("VisualRegressionTracker", () => {
buildId,
projectId,
branchName: config.branchName,
baselineBranchName: config.baselineBranchName,
...testRunMultipart,
});
expect(vrt["submitTestRunMultipart"]).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -310,6 +314,7 @@ describe("VisualRegressionTracker", () => {
buildId,
projectId,
branchName: config.branchName,
baselineBranchName: config.baselineBranchName,
...testRunMultipart,
});
expect(vrt["submitTestRunMultipart"]).toHaveBeenCalledTimes(4);
Expand All @@ -333,6 +338,7 @@ describe("VisualRegressionTracker", () => {
buildId,
projectId,
branchName: config.branchName,
baselineBranchName: config.baselineBranchName,
...testRunBuffer,
});
expect(vrt["submitTestRunMultipart"]).toHaveBeenCalledWith(data);
Expand All @@ -359,6 +365,7 @@ describe("VisualRegressionTracker", () => {
`${config.apiUrl}/builds`,
{
branchName: config.branchName,
baselineBranchName: config.baselineBranchName,
project: config.project,
ciBuildId: config.ciBuildId,
},
Expand Down Expand Up @@ -460,6 +467,7 @@ describe("VisualRegressionTracker", () => {
buildId: buildId,
projectId: projectId,
branchName: config.branchName,
baselineBranchName: config.baselineBranchName,
name: testRunBase64.name,
imageBase64: testRunBase64.imageBase64,
os: testRunBase64.os,
Expand Down
45 changes: 27 additions & 18 deletions lib/visualRegressionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
instanceOfTestRunBuffer,
bufferDtoToFormData,
trackWithRetry,
instanceOfTestRunMultipart,
} from "./helpers";

export class VisualRegressionTracker {
Expand Down Expand Up @@ -56,6 +57,7 @@ export class VisualRegressionTracker {
async start(): Promise<BuildResponse> {
const data = {
branchName: this.config.branchName,
baselineBranchName: this.config.baselineBranchName,
project: this.config.project,
ciBuildId: this.config.ciBuildId,
};
Expand Down Expand Up @@ -93,6 +95,7 @@ export class VisualRegressionTracker {
buildId: this.buildId,
projectId: this.projectId,
branchName: this.config.branchName,
baselineBranchName: this.config.baselineBranchName,
...test,
};

Expand Down Expand Up @@ -138,6 +141,29 @@ export class VisualRegressionTracker {
}
}

private getFormData(
test: TestRunBase64 | TestRunMultipart | TestRunBuffer
): FormData {
if (instanceOfTestRunBuffer(test)) {
return bufferDtoToFormData({
buildId: this.buildId,
projectId: this.projectId,
branchName: this.config.branchName,
baselineBranchName: this.config.baselineBranchName,
...test,
});
} else if (instanceOfTestRunMultipart(test)) {
return multipartDtoToFormData({
buildId: this.buildId,
projectId: this.projectId,
branchName: this.config.branchName,
baselineBranchName: this.config.baselineBranchName,
...test,
});
}
throw new Error("Invalid test run data");
}

/**
* Submit test data to external VRT service
*
Expand All @@ -161,25 +187,8 @@ export class VisualRegressionTracker {
this.config.enableSoftAssert
);
} else {
let formData: FormData;
if (instanceOfTestRunBuffer(test)) {
formData = bufferDtoToFormData({
buildId: this.buildId,
projectId: this.projectId,
branchName: this.config.branchName,
...test,
});
} else {
formData = multipartDtoToFormData({
buildId: this.buildId,
projectId: this.projectId,
branchName: this.config.branchName,
...test,
});
}

testRunResponse = await trackWithRetry(
() => this.submitTestRunMultipart(formData),
() => this.submitTestRunMultipart(this.getFormData(test)),
retryCount,
this.config.enableSoftAssert
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@visual-regression-tracker/sdk-js",
"version": "5.0.0",
"version": "5.5.1",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 40faea0

Please sign in to comment.