Skip to content

Commit fd21f0e

Browse files
committed
refactoring
1 parent a914b31 commit fd21f0e

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

lib/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from "./response/buildResponse";
21
export * from "./config";
32
export * from "./testRun";
3+
export * from "./response/buildResponse";
44
export * from "./response/testRunResponse";
55
export * from "./testStatus";

lib/visualRegressionTracker.spec.ts

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { VisualRegressionTracker } from "./visualRegressionTracker";
2-
import { Config, BuildResponse, TestRun, TestRunResponse, TestStatus } from "./types";
2+
import {
3+
Config,
4+
BuildResponse,
5+
TestRun,
6+
TestRunResponse,
7+
TestStatus,
8+
} from "./types";
39
import { mocked } from "ts-jest/utils";
410
import TestRunResult from "./testRunResult";
511
import axios, { AxiosError, AxiosResponse } from "axios";
@@ -137,60 +143,17 @@ describe("VisualRegressionTracker", () => {
137143
vrt["submitTestResult"] = jest
138144
.fn()
139145
.mockResolvedValueOnce(testRunResponse);
146+
vrt["processTestRun"] = jest.fn();
140147

141148
await vrt.track(testRun);
142149

143150
expect(vrt["submitTestResult"]).toHaveBeenCalledWith(testRun);
151+
expect(vrt["processTestRun"]).toHaveBeenCalledWith(testRunResponse);
144152
expect(mockedTestRunResult).toHaveBeenCalledWith(
145153
testRunResponse,
146154
"http://localhost:4200"
147155
);
148156
});
149-
150-
describe.each<[TestStatus.new | TestStatus.unresolved, string]>([
151-
[TestStatus.new, "No baseline: "],
152-
[TestStatus.unresolved, "Difference found: "],
153-
])("should track error", (status, expectedMessage) => {
154-
const testRunResponseMock: TestRunResponse = {
155-
url: "http://foo.bar",
156-
status: TestStatus.ok,
157-
pixelMisMatchCount: 12,
158-
diffPercent: 0.12,
159-
diffTollerancePercent: 0,
160-
id: "some id",
161-
imageName: "imageName",
162-
merge: false,
163-
};
164-
165-
beforeEach(() => {
166-
testRunResponseMock.status = status;
167-
});
168-
169-
it(`disabled soft assert should throw exception if status ${status}`, async () => {
170-
vrt["config"].enableSoftAssert = false;
171-
vrt["submitTestResult"] = jest
172-
.fn()
173-
.mockResolvedValueOnce(testRunResponseMock);
174-
175-
await expect(vrt.track(testRun)).rejects.toThrowError(
176-
new Error(expectedMessage.concat(testRunResponseMock.url))
177-
);
178-
});
179-
180-
it(`enabled soft assert should log error if status ${status}`, async () => {
181-
console.error = jest.fn();
182-
vrt["config"].enableSoftAssert = true;
183-
vrt["submitTestResult"] = jest
184-
.fn()
185-
.mockResolvedValueOnce(testRunResponseMock);
186-
187-
await vrt.track(testRun);
188-
189-
expect(console.error).toHaveBeenCalledWith(
190-
expectedMessage.concat(testRunResponseMock.url)
191-
);
192-
});
193-
});
194157
});
195158

196159
describe("start", () => {
@@ -396,4 +359,43 @@ describe("VisualRegressionTracker", () => {
396359
);
397360
});
398361
});
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+
});
399401
});

lib/visualRegressionTracker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
TestRunResponse,
66
TestStatus,
77
} from "./types";
8-
import TestRunResult from "./testRunResult"
8+
import TestRunResult from "./testRunResult";
99
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from "axios";
1010

1111
export class VisualRegressionTracker {
@@ -99,6 +99,12 @@ export class VisualRegressionTracker {
9999
async track(test: TestRun): Promise<TestRunResult> {
100100
const testRunResponse = await this.submitTestResult(test);
101101

102+
this.processTestRun(testRunResponse);
103+
104+
return new TestRunResult(testRunResponse, this.config.apiUrl);
105+
}
106+
107+
private processTestRun(testRunResponse: TestRunResponse): void {
102108
let errorMessage: string | undefined;
103109
switch (testRunResponse.status) {
104110
case TestStatus.new: {
@@ -107,6 +113,7 @@ export class VisualRegressionTracker {
107113
}
108114
case TestStatus.unresolved: {
109115
errorMessage = `Difference found: ${testRunResponse.url}`;
116+
break;
110117
}
111118
}
112119

@@ -118,7 +125,5 @@ export class VisualRegressionTracker {
118125
throw new Error(errorMessage);
119126
}
120127
}
121-
122-
return new TestRunResult(testRunResponse, this.config.apiUrl);
123128
}
124129
}

0 commit comments

Comments
 (0)