-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtask.hook.test.ts
245 lines (218 loc) · 7.52 KB
/
task.hook.test.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { it, expect, describe, beforeEach, afterEach } from "vitest";
import path from "path";
import { promises as fs, existsSync } from "fs";
import { dir, file, setGracefulCleanup, withFile } from "tmp-promise";
import {
approveImageTask,
compareImagesTask,
doesFileExistTask,
getScreenshotPathInfoTask,
CompareImagesCfg,
cleanupImagesTask,
} from "./task.hook";
import { generateScreenshotPath } from "./screenshotPath.utils";
import { IMAGE_SNAPSHOT_PREFIX } from "./constants";
setGracefulCleanup();
const fixturesPath = path.resolve(__dirname, "..", "__tests__", "fixtures");
const oldImgFixture = "screenshot.png";
const newImgFixture = "screenshot.actual.png";
const newFileContent = "new file content";
const generateConfig = async (cfg: Partial<CompareImagesCfg>) => ({
updateImages: false,
scaleFactor: 1,
title: "some title",
imgNew: await writeTmpFixture((await file()).path, newImgFixture),
imgOld: await writeTmpFixture((await file()).path, oldImgFixture),
maxDiffThreshold: 0.5,
diffConfig: {},
...cfg,
});
const writeTmpFixture = async (pathToWriteTo: string, fixtureName: string) => {
await fs.mkdir(path.dirname(pathToWriteTo), { recursive: true });
await fs.writeFile(
pathToWriteTo,
await fs.readFile(path.join(fixturesPath, fixtureName))
);
return pathToWriteTo;
};
describe("getScreenshotPathInfoTask", () => {
const specPath = "some/nested/spec-path/spec.ts";
it("returns sanitized path and title", () => {
expect(
getScreenshotPathInfoTask({
titleFromOptions: "some-title-withśpęćiał人物",
imagesPath: "nested/images/dir",
specPath,
currentRetryNumber: 0,
})
).toEqual({
screenshotPath:
"__cp-visual-regression-diff_snapshots__/nested/images/dir/some-title-withśpęćiał人物_#0.actual.png",
title: "some-title-withśpęćiał人物_#0.actual",
});
});
it("supports {spec_path} variable", () => {
expect(
getScreenshotPathInfoTask({
titleFromOptions: "some-title",
imagesPath: "{spec_path}/images/dir",
specPath,
currentRetryNumber: 0,
})
).toEqual({
screenshotPath:
"__cp-visual-regression-diff_snapshots__/some/nested/spec-path/images/dir/some-title_#0.actual.png",
title: "some-title_#0.actual",
});
});
it("supports OS-specific absolute paths", () => {
expect(
getScreenshotPathInfoTask({
titleFromOptions: "some-title",
imagesPath: "/images/dir",
specPath,
currentRetryNumber: 0,
})
).toEqual({
screenshotPath:
"__cp-visual-regression-diff_snapshots__/{unix_system_root_path}/images/dir/some-title_#0.actual.png",
title: "some-title_#0.actual",
});
expect(
getScreenshotPathInfoTask({
titleFromOptions: "some-title",
imagesPath: "C:/images/dir",
specPath,
currentRetryNumber: 0,
})
).toEqual({
screenshotPath:
"__cp-visual-regression-diff_snapshots__/{win_system_root_path}/C/images/dir/some-title_#0.actual.png",
title: "some-title_#0.actual",
});
});
});
describe("cleanupImagesTask", () => {
describe("when env is set", () => {
const generateUsedScreenshotPath = async (projectRoot: string) => {
const screenshotPathWithPrefix = generateScreenshotPath({
titleFromOptions: "some-file",
imagesPath: "images",
specPath: "some/spec/path",
currentRetryNumber: 0,
});
return path.join(
projectRoot,
screenshotPathWithPrefix.substring(
IMAGE_SNAPSHOT_PREFIX.length + path.sep.length
)
);
};
it("does not remove used screenshot", async () => {
const { path: projectRoot } = await dir();
const screenshotPath = await writeTmpFixture(
await generateUsedScreenshotPath(projectRoot),
oldImgFixture
);
cleanupImagesTask({
projectRoot,
env: { pluginVisualRegressionCleanupUnusedImages: true },
} as unknown as Cypress.PluginConfigOptions);
expect(existsSync(screenshotPath)).toBe(true);
});
it("removes unused screenshot", async () => {
const { path: projectRoot } = await dir();
const screenshotPath = await writeTmpFixture(
path.join(projectRoot, "some-file-2_#0.png"),
oldImgFixture
);
cleanupImagesTask({
projectRoot,
env: { pluginVisualRegressionCleanupUnusedImages: true },
} as unknown as Cypress.PluginConfigOptions);
expect(existsSync(screenshotPath)).toBe(false);
});
});
});
describe("approveImageTask", () => {
let newImgPath: string;
let oldImgPath: string;
let diffImgPath: string;
beforeEach(() =>
withFile(async ({ path }) => {
oldImgPath = path;
newImgPath = `${oldImgPath}.actual`;
await fs.writeFile(newImgPath, newFileContent);
diffImgPath = `${oldImgPath}.diff`;
await fs.writeFile(diffImgPath, "");
})
);
afterEach(async () => {
existsSync(diffImgPath) && (await fs.unlink(diffImgPath));
existsSync(newImgPath) && (await fs.unlink(newImgPath));
});
it("removes diff image and replaces old with new", async () => {
approveImageTask({ img: newImgPath });
expect((await fs.readFile(oldImgPath)).toString()).toBe(newFileContent);
expect(existsSync(newImgPath)).toBe(false);
expect(existsSync(diffImgPath)).toBe(false);
});
});
describe("compareImagesTask", () => {
describe("when images should be updated", () => {
describe("when old screenshot exists", () => {
it("resolves with a success message", async () =>
expect(
compareImagesTask(await generateConfig({ updateImages: true }))
).resolves.toEqual({
message:
"Image diff factor (0%) is within boundaries of maximum threshold option 0.5.",
imgDiff: 0,
imgDiffBase64: "",
imgNewBase64: "",
imgOldBase64: "",
maxDiffThreshold: 0.5,
}));
});
});
describe("when images shouldn't be updated", () => {
describe("when old screenshot doesn't exist", () => {
it("resolves with a success message", async () => {
const cfg = await generateConfig({ updateImages: false });
await fs.unlink(cfg.imgOld);
await expect(compareImagesTask(cfg)).resolves.toEqual({
message:
"Image diff factor (0%) is within boundaries of maximum threshold option 0.5.",
imgDiff: 0,
imgDiffBase64: "",
imgNewBase64: "",
imgOldBase64: "",
maxDiffThreshold: 0.5,
});
});
});
describe("when old screenshot exists", () => {
describe("when new image has different resolution", () => {
it("resolves with an error message", async () => {
const cfg = await generateConfig({ updateImages: false });
await expect(compareImagesTask(cfg)).resolves.toMatchSnapshot();
});
});
describe("when new image is exactly the same as old one", () => {
it("resolves with a success message", async () => {
const cfg = await generateConfig({ updateImages: false });
await writeTmpFixture(cfg.imgNew, oldImgFixture);
await expect(compareImagesTask(cfg)).resolves.toMatchSnapshot();
});
});
});
});
});
describe("doesFileExistsTask", () => {
it("checks whether file exists", () => {
expect(doesFileExistTask({ path: "some/random/path" })).toBe(false);
expect(
doesFileExistTask({ path: path.join(fixturesPath, oldImgFixture) })
).toBe(true);
});
});