-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.js
71 lines (67 loc) · 1.71 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { readFileSync, unlinkSync } from "fs";
import { VisualRegressionTracker } from "@visual-regression-tracker/sdk-js";
export function addVisualRegressionTrackerPlugin(on, config) {
const vrtConfig = config?.env?.visualRegressionTracker;
let vrt = new VisualRegressionTracker(vrtConfig);
on("task", {
["ENCODE_IMAGE"]: (props) => {
const { imagePath } = props;
const buffer = readFileSync(imagePath);
const imageBase64 = buffer.toString("base64");
unlinkSync(imagePath);
return imageBase64;
},
["VRT_START"]: async (props) => {
try {
if (!vrt["isStarted"]()) {
await vrt.start();
}
} catch (err) {
return err.message ? err.message : err;
}
return null;
},
["VRT_STOP"]: async (props) => {
try {
if (vrt["isStarted"]()) {
await vrt.stop();
}
} catch (err) {
return err.message ? err.message : err;
}
return null;
},
["TRACK_IMAGE"]: async (props) => {
const {
name,
imageBase64,
browser,
pixelRatio,
os,
device,
diffTollerancePercent,
ignoreAreas,
} = props;
if (!imageBase64) {
throw new Error("Image is missing or not encoded");
}
try {
await vrt.track({
name,
imageBase64,
browser,
viewport: `${config.viewportWidth * pixelRatio}x${
config.viewportHeight * pixelRatio
}`,
os,
device,
diffTollerancePercent,
ignoreAreas,
});
} catch (err) {
return err.message ? err.message : err;
}
return null;
},
});
}