Skip to content

Commit 693f915

Browse files
authored
Merge pull request #36 from Visual-Regression-Tracker/130-test-run-result-added
TestRunResult added
2 parents d603f17 + 81d6ec2 commit 693f915

File tree

11 files changed

+185
-77
lines changed

11 files changed

+185
-77
lines changed

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./visualRegressionTracker";
2+
export * from "./testRunResult";
23
export * from "./types";

lib/testRunResult.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import TestRunResult from "./testRunResult";
2+
import { TestRunResponse, TestStatus } from "./types";
3+
4+
describe("TestRunResult", () => {
5+
it("only required images", () => {
6+
const testRunResponse: TestRunResponse = {
7+
url: "url",
8+
status: TestStatus.ok,
9+
pixelMisMatchCount: 12,
10+
diffPercent: 0.12,
11+
diffTollerancePercent: 0,
12+
id: "some id",
13+
imageName: "imageName",
14+
merge: false,
15+
};
16+
17+
const result = new TestRunResult(testRunResponse, "http://localhost");
18+
19+
expect(result.testRunResponse).toBe(testRunResponse);
20+
expect(result.imageUrl).toBe("http://localhost/imageName");
21+
expect(result.diffUrl).toBeUndefined();
22+
expect(result.baselineUrl).toBeUndefined();
23+
});
24+
25+
it("all image", () => {
26+
const testRunResponse: TestRunResponse = {
27+
url: "url",
28+
status: TestStatus.ok,
29+
pixelMisMatchCount: 12,
30+
diffPercent: 0.12,
31+
diffTollerancePercent: 0,
32+
id: "some id",
33+
imageName: "imageName",
34+
diffName: "diffName",
35+
baselineName: "baselineName",
36+
merge: false,
37+
};
38+
39+
const result = new TestRunResult(testRunResponse, "http://localhost");
40+
41+
expect(result.testRunResponse).toBe(testRunResponse);
42+
expect(result.imageUrl).toBe("http://localhost/imageName");
43+
expect(result.diffUrl).toBe("http://localhost/diffName");
44+
expect(result.baselineUrl).toBe("http://localhost/baselineName");
45+
});
46+
});

lib/testRunResult.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TestRunResponse } from "./types";
2+
3+
export default class TestRunResult {
4+
testRunResponse: TestRunResponse;
5+
imageUrl: string;
6+
diffUrl?: string;
7+
baselineUrl?: string;
8+
9+
constructor(testRunResponse: TestRunResponse, apiUrl: string) {
10+
this.testRunResponse = testRunResponse;
11+
this.imageUrl = apiUrl.concat("/").concat(testRunResponse.imageName);
12+
this.diffUrl =
13+
testRunResponse.diffName &&
14+
apiUrl.concat("/").concat(testRunResponse.diffName);
15+
this.baselineUrl =
16+
testRunResponse.baselineName &&
17+
apiUrl.concat("/").concat(testRunResponse.baselineName);
18+
}
19+
}

lib/types/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from "./build";
21
export * from "./config";
32
export * from "./testRun";
4-
export * from "./testRunResult";
5-
export * from "./testRunStatus";
3+
export * from "./response/buildResponse";
4+
export * from "./response/testRunResponse";
5+
export * from "./testStatus";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface Build {
1+
export interface BuildResponse {
22
id: string;
33
projectId: string;
44
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { TestStatus } from "../testStatus";
2+
3+
export interface TestRunResponse {
4+
id: string;
5+
imageName: string;
6+
diffName?: string;
7+
baselineName?: string;
8+
diffPercent: number;
9+
diffTollerancePercent?: number;
10+
pixelMisMatchCount?: number;
11+
status: TestStatus;
12+
url: string;
13+
merge: boolean;
14+
}

lib/types/testRunResult.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/types/testRunStatus.ts renamed to lib/types/testStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export enum TestRunStatus {
1+
export enum TestStatus {
22
new = "new",
33
ok = "ok",
44
unresolved = "unresolved",

lib/visualRegressionTracker.spec.ts

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { VisualRegressionTracker } from "./visualRegressionTracker";
2-
import { Config, Build, TestRun, TestRunResult, TestRunStatus } from "./types";
2+
import {
3+
Config,
4+
BuildResponse,
5+
TestRun,
6+
TestRunResponse,
7+
TestStatus,
8+
} from "./types";
39
import { mocked } from "ts-jest/utils";
10+
import TestRunResult from "./testRunResult";
411
import axios, { AxiosError, AxiosResponse } from "axios";
512

613
jest.mock("axios");
714
const mockedAxios = mocked(axios, true);
815

16+
jest.mock("./testRunResult");
17+
const mockedTestRunResult = mocked(TestRunResult, true);
18+
919
const axiosError404: AxiosError = {
1020
isAxiosError: true,
1121
config: {},
@@ -118,68 +128,39 @@ describe("VisualRegressionTracker", () => {
118128
};
119129

120130
it("should track success", async () => {
121-
const testRunResult: TestRunResult = {
131+
const testRunResponse: TestRunResponse = {
122132
url: "url",
123-
status: TestRunStatus.ok,
133+
status: TestStatus.ok,
124134
pixelMisMatchCount: 12,
125135
diffPercent: 0.12,
126136
diffTollerancePercent: 0,
137+
id: "some id",
138+
imageName: "imageName",
139+
diffName: "diffName",
140+
baselineName: "baselineName",
141+
merge: false,
127142
};
128-
vrt["submitTestResult"] = jest.fn().mockResolvedValueOnce(testRunResult);
143+
vrt["submitTestResult"] = jest
144+
.fn()
145+
.mockResolvedValueOnce(testRunResponse);
146+
vrt["processTestRun"] = jest.fn();
129147

130148
await vrt.track(testRun);
131149

132150
expect(vrt["submitTestResult"]).toHaveBeenCalledWith(testRun);
133-
});
134-
135-
describe.each<[TestRunStatus.new | TestRunStatus.unresolved, string]>([
136-
[TestRunStatus.new, "No baseline: "],
137-
[TestRunStatus.unresolved, "Difference found: "],
138-
])("should track error", (status, expectedMessage) => {
139-
const testRunResultMock: TestRunResult = {
140-
url: "http://foo.bar",
141-
status: TestRunStatus.ok,
142-
pixelMisMatchCount: 12,
143-
diffPercent: 0.12,
144-
diffTollerancePercent: 0,
145-
};
146-
147-
beforeEach(() => {
148-
testRunResultMock.status = status;
149-
});
150-
151-
it(`disabled soft assert should throw exception if status ${status}`, async () => {
152-
vrt["config"].enableSoftAssert = false;
153-
vrt["submitTestResult"] = jest
154-
.fn()
155-
.mockResolvedValueOnce(testRunResultMock);
156-
157-
await expect(vrt.track(testRun)).rejects.toThrowError(
158-
new Error(expectedMessage.concat(testRunResultMock.url))
159-
);
160-
});
161-
162-
it(`enabled soft assert should log error if status ${status}`, async () => {
163-
console.error = jest.fn();
164-
vrt["config"].enableSoftAssert = true;
165-
vrt["submitTestResult"] = jest
166-
.fn()
167-
.mockResolvedValueOnce(testRunResultMock);
168-
169-
await vrt.track(testRun);
170-
171-
expect(console.error).toHaveBeenCalledWith(
172-
expectedMessage.concat(testRunResultMock.url)
173-
);
174-
});
151+
expect(vrt["processTestRun"]).toHaveBeenCalledWith(testRunResponse);
152+
expect(mockedTestRunResult).toHaveBeenCalledWith(
153+
testRunResponse,
154+
"http://localhost:4200"
155+
);
175156
});
176157
});
177158

178159
describe("start", () => {
179160
test("should start build", async () => {
180161
const buildId = "1312";
181162
const projectId = "asd";
182-
const build: Build = {
163+
const build: BuildResponse = {
183164
id: buildId,
184165
projectId: projectId,
185166
};
@@ -261,12 +242,15 @@ describe("VisualRegressionTracker", () => {
261242

262243
describe("submitTestResults", () => {
263244
it("should submit test run", async () => {
264-
const testRunResult: TestRunResult = {
245+
const testRunResponse: TestRunResponse = {
265246
url: "url",
266-
status: TestRunStatus.unresolved,
247+
status: TestStatus.unresolved,
267248
pixelMisMatchCount: 12,
268249
diffPercent: 0.12,
269250
diffTollerancePercent: 0,
251+
id: "some id",
252+
imageName: "imageName",
253+
merge: false,
270254
};
271255
const testRun: TestRun = {
272256
name: "name",
@@ -280,11 +264,11 @@ describe("VisualRegressionTracker", () => {
280264
const projectId = "asd";
281265
vrt["buildId"] = buildId;
282266
vrt["projectId"] = projectId;
283-
mockedAxios.post.mockResolvedValueOnce({ data: testRunResult });
267+
mockedAxios.post.mockResolvedValueOnce({ data: testRunResponse });
284268

285269
const result = await vrt["submitTestResult"](testRun);
286270

287-
expect(result).toBe(testRunResult);
271+
expect(result).toBe(testRunResponse);
288272
expect(mockedAxios.post).toHaveBeenCalledWith(
289273
`${config.apiUrl}/test-runs`,
290274
{
@@ -337,7 +321,7 @@ describe("VisualRegressionTracker", () => {
337321
});
338322

339323
test("handleResponse", async () => {
340-
const build: Build = {
324+
const build: BuildResponse = {
341325
id: "id",
342326
projectId: "projectId",
343327
};
@@ -375,4 +359,43 @@ describe("VisualRegressionTracker", () => {
375359
);
376360
});
377361
});
362+
363+
describe.each<[TestStatus.new | TestStatus.unresolved, string]>([
364+
[TestStatus.new, "No baseline: "],
365+
[TestStatus.unresolved, "Difference found: "],
366+
])("processTestRun", (status, expectedMessage) => {
367+
const testRunResponse: TestRunResponse = {
368+
url: "http://foo.bar",
369+
status: TestStatus.ok,
370+
pixelMisMatchCount: 12,
371+
diffPercent: 0.12,
372+
diffTollerancePercent: 0,
373+
id: "some id",
374+
imageName: "imageName",
375+
merge: false,
376+
};
377+
378+
beforeEach(() => {
379+
testRunResponse.status = status;
380+
});
381+
382+
it(`disabled soft assert should throw exception if status ${status}`, () => {
383+
vrt["config"].enableSoftAssert = false;
384+
385+
expect(() => vrt["processTestRun"](testRunResponse)).toThrowError(
386+
new Error(expectedMessage.concat(testRunResponse.url))
387+
);
388+
});
389+
390+
it(`enabled soft assert should log error if status ${status}`, () => {
391+
console.error = jest.fn();
392+
vrt["config"].enableSoftAssert = true;
393+
394+
vrt["processTestRun"](testRunResponse);
395+
396+
expect(console.error).toHaveBeenCalledWith(
397+
expectedMessage.concat(testRunResponse.url)
398+
);
399+
});
400+
});
378401
});

lib/visualRegressionTracker.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Config, Build, TestRun, TestRunResult, TestRunStatus } from "./types";
1+
import {
2+
Config,
3+
BuildResponse,
4+
TestRun,
5+
TestRunResponse,
6+
TestStatus,
7+
} from "./types";
8+
import TestRunResult from "./testRunResult";
29
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from "axios";
310

411
export class VisualRegressionTracker {
@@ -26,7 +33,7 @@ export class VisualRegressionTracker {
2633
project: this.config.project,
2734
};
2835

29-
const build: Build = await axios
36+
const build: BuildResponse = await axios
3037
.post(`${this.config.apiUrl}/builds`, data, this.axiosConfig)
3138
.then(this.handleResponse)
3239
.catch(this.handleException);
@@ -50,7 +57,7 @@ export class VisualRegressionTracker {
5057
.catch(this.handleException);
5158
}
5259

53-
private async submitTestResult(test: TestRun): Promise<TestRunResult> {
60+
private async submitTestResult(test: TestRun): Promise<TestRunResponse> {
5461
if (!this.isStarted()) {
5562
throw new Error("Visual Regression Tracker has not been started");
5663
}
@@ -89,17 +96,24 @@ export class VisualRegressionTracker {
8996
}
9097
}
9198

92-
async track(test: TestRun) {
93-
const result = await this.submitTestResult(test);
99+
async track(test: TestRun): Promise<TestRunResult> {
100+
const testRunResponse = await this.submitTestResult(test);
94101

102+
this.processTestRun(testRunResponse);
103+
104+
return new TestRunResult(testRunResponse, this.config.apiUrl);
105+
}
106+
107+
private processTestRun(testRunResponse: TestRunResponse): void {
95108
let errorMessage: string | undefined;
96-
switch (result.status) {
97-
case TestRunStatus.new: {
98-
errorMessage = `No baseline: ${result.url}`;
109+
switch (testRunResponse.status) {
110+
case TestStatus.new: {
111+
errorMessage = `No baseline: ${testRunResponse.url}`;
99112
break;
100113
}
101-
case TestRunStatus.unresolved: {
102-
errorMessage = `Difference found: ${result.url}`;
114+
case TestStatus.unresolved: {
115+
errorMessage = `Difference found: ${testRunResponse.url}`;
116+
break;
103117
}
104118
}
105119

0 commit comments

Comments
 (0)