Skip to content

Commit d5ac83b

Browse files
authored
feat: Migrate to multipart upload instead of base64 (#113)
* feat: Migrate to multipart upload instead of base64 closes #112 BREAKING CHANGE: min service api version required `4.14.0`
1 parent f720d0b commit d5ac83b

File tree

7 files changed

+256
-215
lines changed

7 files changed

+256
-215
lines changed

cypress.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
"visualRegressionTracker": {
88
"apiUrl": "http://localhost:4200",
99
"apiKey": "5H90NFWM6BMWWDMWKG8T11DWW22Y",
10-
"project": "e999e73d-d683-4311-928c-d6f8fe82e4f8",
10+
"project": "fcf9ab05-4bed-40d6-a033-c178948c9897",
1111
"branchName": "master",
12-
"enableSoftAssert": true,
13-
"ciBuildId": null
12+
"enableSoftAssert": false
1413
}
1514
}
1615
}

cypress/integration/example.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
/* global cy */
22

3+
before(() => {
4+
cy.vrtStart();
5+
});
6+
7+
after(() => {
8+
cy.vrtStop();
9+
});
10+
311
context("Visual Regression Tracker", () => {
412
beforeEach(() => {
513
cy.visit("/commands/viewport");

lib/commands.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
checkResult,
66
trackImage,
77
trackWithRetry,
8+
handleError,
89
} from "./utils";
910

1011
export const addVrtStartCommand = () => {
@@ -15,11 +16,7 @@ export const addVrtStartCommand = () => {
1516
},
1617
() => {
1718
cy.task("VRT_START", {}, { log: false })
18-
.then((err) => {
19-
if (err) {
20-
throw new Error(err as string);
21-
}
22-
})
19+
.then(handleError)
2320
.then(() => log("Started"));
2421
}
2522
);
@@ -33,11 +30,7 @@ export const addVrtStopCommand = () => {
3330
},
3431
() => {
3532
cy.task("VRT_STOP", {}, { log: false })
36-
.then((err) => {
37-
if (err) {
38-
throw new Error(err as string);
39-
}
40-
})
33+
.then(handleError)
4134
.then(() => log("Stopped"));
4235
}
4336
);

lib/plugin.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
import { readFileSync, unlinkSync } from "fs";
1+
import { unlinkSync } from "fs";
22
import { VisualRegressionTracker } from "@visual-regression-tracker/sdk-js";
33

44
export function addVisualRegressionTrackerPlugin(on, config) {
55
const vrtConfig = config?.env?.visualRegressionTracker;
66
let vrt = new VisualRegressionTracker(vrtConfig);
77

88
on("task", {
9-
["ENCODE_IMAGE"]: (props) => {
10-
const { imagePath } = props;
11-
const buffer = readFileSync(imagePath);
12-
const imageBase64 = buffer.toString("base64");
13-
unlinkSync(imagePath);
14-
return imageBase64;
15-
},
169
["VRT_START"]: async (props) => {
1710
try {
1811
if (!vrt["isStarted"]()) {
1912
await vrt.start();
2013
}
2114
} catch (err) {
22-
return err.message ? err.message : err;
15+
return err.message ?? err;
2316
}
2417
return null;
2518
},
@@ -29,40 +22,42 @@ export function addVisualRegressionTrackerPlugin(on, config) {
2922
await vrt.stop();
3023
}
3124
} catch (err) {
32-
return err.message ? err.message : err;
25+
return err.message ?? err;
3326
}
3427
return null;
3528
},
36-
["VRT_TRACK_IMAGE"]: async (props) => {
29+
["VRT_TRACK_IMAGE_MULTIPART"]: async (props) => {
3730
const {
3831
name,
39-
imageBase64,
32+
imagePath,
4033
browser,
4134
viewport,
42-
pixelRatio,
4335
os,
4436
device,
4537
diffTollerancePercent,
4638
ignoreAreas,
4739
} = props;
4840

49-
if (!imageBase64) {
50-
throw new Error("Image is missing or not encoded");
51-
}
52-
53-
return vrt["submitTestResult"]({
41+
const result = await vrt["submitTestRunMultipart"]({
5442
name,
55-
imageBase64,
43+
imagePath,
5644
browser,
5745
viewport,
5846
os,
5947
device,
6048
diffTollerancePercent,
6149
ignoreAreas,
6250
});
51+
52+
unlinkSync(imagePath);
53+
return result;
6354
},
6455
["VRT_PROCESS_ERROR_RESULT"]: async (testRunResult) => {
65-
vrt["processTestRun"](testRunResult);
56+
try {
57+
vrt["processTestRun"](testRunResult);
58+
} catch (err) {
59+
return err.message ?? err;
60+
}
6661
return null;
6762
},
6863
});

lib/utils.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ export const log = (message: string) =>
99
message,
1010
});
1111

12+
export const handleError = (err: unknown) => {
13+
if (err) {
14+
throw new Error(err as string);
15+
}
16+
};
17+
18+
export const toTestRunDto = ({
19+
name,
20+
pixelRatio,
21+
options,
22+
}: {
23+
name: string;
24+
pixelRatio: number;
25+
options: any;
26+
}) => ({
27+
name,
28+
browser: Cypress.browser.name,
29+
viewport:
30+
options?.viewport ??
31+
`${Cypress.config("viewportWidth") * pixelRatio}x${
32+
Cypress.config("viewportHeight") * pixelRatio
33+
}`,
34+
os: options?.os,
35+
device: options?.device,
36+
diffTollerancePercent: options?.diffTollerancePercent,
37+
ignoreAreas: options?.ignoreAreas,
38+
});
39+
1240
export const trackWithRetry = (
1341
trackFn: () => Cypress.Chainable<TestRunResponse>,
1442
shouldStopFn: (result: TestRunResponse) => boolean,
@@ -27,7 +55,7 @@ export const trackWithRetry = (
2755
};
2856

2957
export const checkResult = (result: TestRunResponse) =>
30-
cy.task("VRT_PROCESS_ERROR_RESULT", result, { log: false });
58+
cy.task("VRT_PROCESS_ERROR_RESULT", result, { log: false }).then(handleError);
3159

3260
export const shouldStopRetry = (result: TestRunResponse) =>
3361
result?.status !== TestStatus.unresolved;
@@ -51,24 +79,12 @@ export const trackImage = (
5179
},
5280
})
5381
.then(() => log(`tracking ${name}`))
54-
.then(() => cy.task("ENCODE_IMAGE", { imagePath }, { log: false }))
55-
.then((imageBase64) =>
82+
.then(() =>
5683
cy.task(
57-
"VRT_TRACK_IMAGE",
84+
"VRT_TRACK_IMAGE_MULTIPART",
5885
{
59-
name,
60-
imageBase64,
61-
browser: Cypress.browser.name,
62-
viewport:
63-
options?.viewport ??
64-
`${Cypress.config("viewportWidth") * pixelRatio}x${
65-
Cypress.config("viewportHeight") * pixelRatio
66-
}`,
67-
pixelRatio,
68-
os: options?.os,
69-
device: options?.device,
70-
diffTollerancePercent: options?.diffTollerancePercent,
71-
ignoreAreas: options?.ignoreAreas,
86+
...toTestRunDto({ name, pixelRatio, options }),
87+
imagePath,
7288
},
7389
{ log: false }
7490
)

0 commit comments

Comments
 (0)