-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: retry added by default
- Loading branch information
Showing
7 changed files
with
804 additions
and
572 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TestStatus, TestRunResponse } from "../types"; | ||
|
||
export const testRunUnresolvedResponse: TestRunResponse = { | ||
url: "url", | ||
status: TestStatus.unresolved, | ||
pixelMisMatchCount: 12, | ||
diffPercent: 0.12, | ||
diffTollerancePercent: 0, | ||
id: "some id", | ||
imageName: "imageName", | ||
merge: false, | ||
}; | ||
|
||
export const testRunOkResponse: TestRunResponse = { | ||
url: "url", | ||
status: TestStatus.ok, | ||
diffPercent: 0, | ||
diffTollerancePercent: 0, | ||
id: "some id", | ||
imageName: "imageName", | ||
merge: false, | ||
}; | ||
|
||
export const testRunNewResponse: TestRunResponse = { | ||
url: "url", | ||
status: TestStatus.new, | ||
pixelMisMatchCount: 0, | ||
diffPercent: 0, | ||
diffTollerancePercent: 0, | ||
id: "some id", | ||
imageName: "imageName", | ||
merge: false, | ||
}; | ||
|
||
export const testRunAutoApprovedResponse: TestRunResponse = { | ||
url: "url", | ||
status: TestStatus.autoApproved, | ||
pixelMisMatchCount: 0, | ||
diffPercent: 0, | ||
diffTollerancePercent: 0, | ||
id: "some id", | ||
imageName: "imageName", | ||
merge: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./config.helper"; | ||
export * from "./type.helper"; | ||
export * from "./dto.helper"; | ||
export * from "./track.helper"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { TestRunResponse, TestStatus } from "../types"; | ||
import { processTestRun, shouldStopRetry } from "./track.helper"; | ||
import { | ||
testRunOkResponse, | ||
testRunUnresolvedResponse, | ||
testRunAutoApprovedResponse, | ||
testRunNewResponse, | ||
} from "../__data__"; | ||
|
||
describe.each<[TestStatus.new | TestStatus.unresolved, string]>([ | ||
[TestStatus.new, "No baseline: "], | ||
[TestStatus.unresolved, "Difference found: "], | ||
])("processTestRun", (status, expectedMessage) => { | ||
beforeEach(() => { | ||
testRunUnresolvedResponse.status = status; | ||
}); | ||
|
||
it(`default soft assert should throw exception if status ${status}`, () => { | ||
expect(() => processTestRun(testRunUnresolvedResponse)).toThrowError( | ||
new Error(expectedMessage.concat(testRunUnresolvedResponse.url)) | ||
); | ||
}); | ||
|
||
it(`disabled soft assert should throw exception if status ${status}`, () => { | ||
const enableSoftAssert = false; | ||
|
||
expect(() => | ||
processTestRun(testRunUnresolvedResponse, enableSoftAssert) | ||
).toThrowError( | ||
new Error(expectedMessage.concat(testRunUnresolvedResponse.url)) | ||
); | ||
}); | ||
|
||
it(`enabled soft assert should log error if status ${status}`, () => { | ||
console.error = jest.fn(); | ||
const enableSoftAssert = true; | ||
|
||
processTestRun(testRunUnresolvedResponse, enableSoftAssert); | ||
|
||
expect(console.error).toHaveBeenCalledWith( | ||
expectedMessage.concat(testRunUnresolvedResponse.url) | ||
); | ||
}); | ||
}); | ||
|
||
it.each<[TestRunResponse, boolean]>([ | ||
[testRunOkResponse, true], | ||
[testRunUnresolvedResponse, false], | ||
[testRunAutoApprovedResponse, true], | ||
[testRunNewResponse, true], | ||
])("shouldStopRetry", (response, expectedResult) => { | ||
expect(shouldStopRetry(response)).toBe(expectedResult); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { TestRunResponse, TestStatus } from "../types"; | ||
|
||
const getErrorMessage = ( | ||
testRunResponse: TestRunResponse | ||
): string | undefined => { | ||
switch (testRunResponse.status) { | ||
case TestStatus.new: { | ||
return `No baseline: ${testRunResponse.url}`; | ||
} | ||
case TestStatus.unresolved: { | ||
return `Difference found: ${testRunResponse.url}`; | ||
} | ||
} | ||
}; | ||
|
||
export const processTestRun = ( | ||
testRunResponse: TestRunResponse, | ||
enableSoftAssert?: boolean | ||
) => { | ||
const errorMessage = getErrorMessage(testRunResponse); | ||
|
||
if (errorMessage) { | ||
if (enableSoftAssert) { | ||
// eslint-disable-next-line no-console | ||
console.error(errorMessage); | ||
} else { | ||
throw new Error(errorMessage); | ||
} | ||
} | ||
}; | ||
|
||
export const shouldStopRetry = (result: TestRunResponse) => | ||
result.status !== TestStatus.unresolved; | ||
|
||
export const trackWithRetry = async ( | ||
trackFn: () => Promise<TestRunResponse>, | ||
retryLimit: number, | ||
enableSoftAssert?: boolean | ||
): Promise<TestRunResponse> => { | ||
const result = await trackFn(); | ||
if (retryLimit <= 0 || shouldStopRetry(result)) { | ||
processTestRun(result, enableSoftAssert); | ||
return result; | ||
} | ||
// eslint-disable-next-line no-console | ||
console.info(`Diff found... Remaining retry attempts **${retryLimit}**`); | ||
return trackWithRetry(trackFn, retryLimit - 1, enableSoftAssert); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.