Skip to content

Commit 40faea0

Browse files
authored
feat: baselineBranchName param added
1 parent 98a79a2 commit 40faea0

File tree

11 files changed

+113
-22
lines changed

11 files changed

+113
-22
lines changed

lib/helpers/config.helper.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe("config.helper", () => {
2828
const updatedConfig: Config = {
2929
apiUrl: "apiUrlUpdated",
3030
branchName: "branchNameUpdated",
31+
baselineBranchName: "baselineBranchNameUpdated",
3132
project: "projectUpdated",
3233
apiKey: "apiKeyUpdated",
3334
enableSoftAssert: false,
@@ -79,6 +80,7 @@ describe("config.helper", () => {
7980
VRT_APIURL: "apiUrlTest",
8081
VRT_CIBUILDID: "ciBuildIdTest",
8182
VRT_BRANCHNAME: "branchNameTest",
83+
VRT_BASELINEBRANCHNAME: "baselineBranchNameTest",
8284
VRT_PROJECT: "projectTest",
8385
VRT_APIKEY: "apiKeyTest",
8486
VRT_ENABLESOFTASSERT: "false",
@@ -90,6 +92,7 @@ describe("config.helper", () => {
9092
apiUrl: "apiUrlTest",
9193
ciBuildId: "ciBuildIdTest",
9294
branchName: "branchNameTest",
95+
baselineBranchName: "baselineBranchNameTest",
9396
project: "projectTest",
9497
apiKey: "apiKeyTest",
9598
enableSoftAssert: false,
@@ -101,6 +104,7 @@ describe("config.helper", () => {
101104
VRT_APIURL: undefined,
102105
VRT_CIBUILDID: undefined,
103106
VRT_BRANCHNAME: undefined,
107+
VRT_BASELINEBRANCHNAME: undefined,
104108
VRT_PROJECT: undefined,
105109
VRT_APIKEY: undefined,
106110
VRT_ENABLESOFTASSERT: undefined,

lib/helpers/config.helper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const readConfigFromFile = (config: Config): Config => {
1212
if (fileConfig.branchName) {
1313
config.branchName = fileConfig.branchName;
1414
}
15+
if (fileConfig.baselineBranchName) {
16+
config.baselineBranchName = fileConfig.baselineBranchName;
17+
}
1518
if (fileConfig.project) {
1619
config.project = fileConfig.project;
1720
}
@@ -35,6 +38,9 @@ const readConfigFromEnv = (config: Config): Config => {
3538
if (process.env["VRT_BRANCHNAME"]) {
3639
config.branchName = process.env["VRT_BRANCHNAME"];
3740
}
41+
if (process.env["VRT_BASELINEBRANCHNAME"]) {
42+
config.baselineBranchName = process.env["VRT_BASELINEBRANCHNAME"];
43+
}
3844
if (process.env["VRT_PROJECT"]) {
3945
config.project = process.env["VRT_PROJECT"];
4046
}

lib/helpers/dto.helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const multipartDtoToFormData = (dto: TestRunMultipartDto): FormData => {
77
data.append("buildId", dto.buildId);
88
data.append("projectId", dto.projectId);
99
data.append("branchName", dto.branchName);
10+
dto.baselineBranchName && data.append("baselineBranchName", dto.baselineBranchName);
1011
data.append("name", dto.name);
1112
data.append("image", fs.createReadStream(dto.imagePath), {
1213
knownLength: fs.statSync(dto.imagePath).size,
@@ -30,6 +31,7 @@ export const bufferDtoToFormData = (dto: TestRunBufferDto): FormData => {
3031
data.append("buildId", dto.buildId);
3132
data.append("projectId", dto.projectId);
3233
data.append("branchName", dto.branchName);
34+
dto.baselineBranchName && data.append("baselineBranchName", dto.baselineBranchName);
3335
data.append("name", dto.name);
3436
data.append("image", dto.imageBuffer, { filename: "image.png" });
3537
dto.os && data.append("os", dto.os);

lib/helpers/track.helper.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { TestRunResponse, TestStatus } from "../types";
2-
import { processTestRun, shouldStopRetry } from "./track.helper";
2+
import {
3+
processTestRun,
4+
shouldStopRetry,
5+
trackWithRetry,
6+
} from "./track.helper";
37
import {
48
testRunOkResponse,
59
testRunUnresolvedResponse,
@@ -43,6 +47,57 @@ describe.each<[TestStatus.new | TestStatus.unresolved, string]>([
4347
});
4448
});
4549

50+
const unresolvedResponce: TestRunResponse = {
51+
id: "someId",
52+
imageName: "imageName",
53+
diffName: "diffName",
54+
baselineName: "baselineName",
55+
diffPercent: 1.11,
56+
diffTollerancePercent: 2.22,
57+
pixelMisMatchCount: 3,
58+
status: TestStatus.unresolved,
59+
url: "url",
60+
merge: true,
61+
};
62+
63+
const okResponce: TestRunResponse = {
64+
...unresolvedResponce,
65+
status: TestStatus.ok,
66+
};
67+
68+
it("should stop when diff not found", async () => {
69+
// .Arrange
70+
const trackMock = jest.fn().mockReturnValue(okResponce);
71+
72+
// .Act
73+
await trackWithRetry(trackMock, 5);
74+
75+
// .Assert
76+
expect(trackMock).toBeCalledTimes(1);
77+
});
78+
79+
it("should stop on default retry limit", async () => {
80+
// .Arrange
81+
const trackMock = jest.fn().mockReturnValue(unresolvedResponce);
82+
83+
// .Act
84+
await trackWithRetry(trackMock, undefined as unknown as number, true);
85+
86+
// .Assert
87+
expect(trackMock).toBeCalledTimes(3);
88+
});
89+
90+
it("should stop on custom retry limit", async () => {
91+
// .Arrange
92+
const trackMock = jest.fn().mockReturnValue(unresolvedResponce);
93+
94+
// .Act
95+
await trackWithRetry(trackMock, 5, true);
96+
97+
// .Assert
98+
expect(trackMock).toBeCalledTimes(6);
99+
});
100+
46101
it.each<[TestRunResponse, boolean]>([
47102
[testRunOkResponse, true],
48103
[testRunUnresolvedResponse, false],

lib/helpers/track.helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const shouldStopRetry = (result: TestRunResponse) =>
3434

3535
export const trackWithRetry = async (
3636
trackFn: () => Promise<TestRunResponse>,
37-
retryLimit: number,
37+
retryLimit: number = 2,
3838
enableSoftAssert?: boolean
3939
): Promise<TestRunResponse> => {
4040
const result = await trackFn();

lib/helpers/type.helper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { TestRunBase64, TestRunBuffer } from "../types";
1+
import { TestRunBase64, TestRunBuffer, TestRunMultipart } from "../types";
22

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

77
export function instanceOfTestRunBuffer(object: any): object is TestRunBuffer {
88
return "imageBuffer" in object;
9+
}
10+
11+
export function instanceOfTestRunMultipart(object: any): object is TestRunMultipart {
12+
return "imagePath" in object;
913
}

lib/types/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface Config extends Record<string, any> {
22
apiUrl: string;
33
branchName: string;
4+
baselineBranchName?: string;
45
project: string;
56
apiKey: string;
67
enableSoftAssert?: boolean;

lib/types/request/testRun.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface TestRunDto {
1515

1616
branchName: string;
1717

18+
baselineBranchName?: string
19+
1820
buildId: string;
1921

2022
projectId: string;

lib/visualRegressionTracker.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const axiosErrorEmptyResponse: AxiosError = {
102102
const config: Config = {
103103
apiUrl: "http://localhost:4200",
104104
branchName: "develop",
105+
baselineBranchName: "next-release",
105106
project: "Default project",
106107
apiKey: "CPKVK4JNK24NVNPNGVFQ853HXXEG",
107108
enableSoftAssert: false,
@@ -176,6 +177,7 @@ describe("VisualRegressionTracker", () => {
176177
const fileConfig: Config = {
177178
apiUrl: "apiUrlFile",
178179
branchName: "branchNameFile",
180+
baselineBranchName: "baselineBranchNameFile",
179181
project: "projectFile",
180182
apiKey: "apiKeyFile",
181183
enableSoftAssert: false,
@@ -184,6 +186,7 @@ describe("VisualRegressionTracker", () => {
184186
const envConfig: Config = {
185187
apiUrl: "apiUrlEnv",
186188
branchName: "branchNameEnv",
189+
baselineBranchName: "baselineBranchNameEnv",
187190
project: "projectEnv",
188191
apiKey: "apiKeyEnv",
189192
enableSoftAssert: false,
@@ -282,6 +285,7 @@ describe("VisualRegressionTracker", () => {
282285
buildId,
283286
projectId,
284287
branchName: config.branchName,
288+
baselineBranchName: config.baselineBranchName,
285289
...testRunMultipart,
286290
});
287291
expect(vrt["submitTestRunMultipart"]).toHaveBeenCalledTimes(1);
@@ -310,6 +314,7 @@ describe("VisualRegressionTracker", () => {
310314
buildId,
311315
projectId,
312316
branchName: config.branchName,
317+
baselineBranchName: config.baselineBranchName,
313318
...testRunMultipart,
314319
});
315320
expect(vrt["submitTestRunMultipart"]).toHaveBeenCalledTimes(4);
@@ -333,6 +338,7 @@ describe("VisualRegressionTracker", () => {
333338
buildId,
334339
projectId,
335340
branchName: config.branchName,
341+
baselineBranchName: config.baselineBranchName,
336342
...testRunBuffer,
337343
});
338344
expect(vrt["submitTestRunMultipart"]).toHaveBeenCalledWith(data);
@@ -359,6 +365,7 @@ describe("VisualRegressionTracker", () => {
359365
`${config.apiUrl}/builds`,
360366
{
361367
branchName: config.branchName,
368+
baselineBranchName: config.baselineBranchName,
362369
project: config.project,
363370
ciBuildId: config.ciBuildId,
364371
},
@@ -460,6 +467,7 @@ describe("VisualRegressionTracker", () => {
460467
buildId: buildId,
461468
projectId: projectId,
462469
branchName: config.branchName,
470+
baselineBranchName: config.baselineBranchName,
463471
name: testRunBase64.name,
464472
imageBase64: testRunBase64.imageBase64,
465473
os: testRunBase64.os,

lib/visualRegressionTracker.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
instanceOfTestRunBuffer,
2121
bufferDtoToFormData,
2222
trackWithRetry,
23+
instanceOfTestRunMultipart,
2324
} from "./helpers";
2425

2526
export class VisualRegressionTracker {
@@ -56,6 +57,7 @@ export class VisualRegressionTracker {
5657
async start(): Promise<BuildResponse> {
5758
const data = {
5859
branchName: this.config.branchName,
60+
baselineBranchName: this.config.baselineBranchName,
5961
project: this.config.project,
6062
ciBuildId: this.config.ciBuildId,
6163
};
@@ -93,6 +95,7 @@ export class VisualRegressionTracker {
9395
buildId: this.buildId,
9496
projectId: this.projectId,
9597
branchName: this.config.branchName,
98+
baselineBranchName: this.config.baselineBranchName,
9699
...test,
97100
};
98101

@@ -138,6 +141,29 @@ export class VisualRegressionTracker {
138141
}
139142
}
140143

144+
private getFormData(
145+
test: TestRunBase64 | TestRunMultipart | TestRunBuffer
146+
): FormData {
147+
if (instanceOfTestRunBuffer(test)) {
148+
return bufferDtoToFormData({
149+
buildId: this.buildId,
150+
projectId: this.projectId,
151+
branchName: this.config.branchName,
152+
baselineBranchName: this.config.baselineBranchName,
153+
...test,
154+
});
155+
} else if (instanceOfTestRunMultipart(test)) {
156+
return multipartDtoToFormData({
157+
buildId: this.buildId,
158+
projectId: this.projectId,
159+
branchName: this.config.branchName,
160+
baselineBranchName: this.config.baselineBranchName,
161+
...test,
162+
});
163+
}
164+
throw new Error("Invalid test run data");
165+
}
166+
141167
/**
142168
* Submit test data to external VRT service
143169
*
@@ -161,25 +187,8 @@ export class VisualRegressionTracker {
161187
this.config.enableSoftAssert
162188
);
163189
} else {
164-
let formData: FormData;
165-
if (instanceOfTestRunBuffer(test)) {
166-
formData = bufferDtoToFormData({
167-
buildId: this.buildId,
168-
projectId: this.projectId,
169-
branchName: this.config.branchName,
170-
...test,
171-
});
172-
} else {
173-
formData = multipartDtoToFormData({
174-
buildId: this.buildId,
175-
projectId: this.projectId,
176-
branchName: this.config.branchName,
177-
...test,
178-
});
179-
}
180-
181190
testRunResponse = await trackWithRetry(
182-
() => this.submitTestRunMultipart(formData),
191+
() => this.submitTestRunMultipart(this.getFormData(test)),
183192
retryCount,
184193
this.config.enableSoftAssert
185194
);

0 commit comments

Comments
 (0)