Skip to content

Commit e3217d0

Browse files
committed
chore: write tests
Signed-off-by: Jakub Freisler <[email protected]>
1 parent 67bb8ce commit e3217d0

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/afterScreenshot.hook.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { it, expect, describe } from "vitest";
22
import path from "path";
33
import { promises as fs, existsSync } from "fs";
4-
import { initAfterScreenshotHook } from "./afterScreenshot.hook";
4+
import { initAfterScreenshotHook, parseAbsolutePath } from "./afterScreenshot.hook";
55
import { dir, file, setGracefulCleanup } from "tmp-promise";
6-
import { IMAGE_SNAPSHOT_PREFIX } from "./constants";
6+
import { IMAGE_SNAPSHOT_PREFIX, PATH_VARIABLES } from "./constants";
77

88
setGracefulCleanup();
99

@@ -31,3 +31,23 @@ describe("initAfterScreenshotHook", () => {
3131
await fs.unlink(expectedNewPath);
3232
});
3333
});
34+
35+
describe('parseAbsolutePath', () => {
36+
const projectRoot = '/its/project/root';
37+
38+
it('resolves relative paths against project root', () => {
39+
expect(parseAbsolutePath({ screenshotPath: 'some/path.png', projectRoot }))
40+
.toBe('/its/project/root/some/path.png');
41+
});
42+
43+
it('builds proper win paths when found', () => {
44+
expect(parseAbsolutePath({ screenshotPath: `${PATH_VARIABLES.winSystemRootPath}/D/some/path.png`, projectRoot }))
45+
// that's expected output accorind to https://stackoverflow.com/a/64135721/8805801
46+
.toBe('D:\\/some/path.png');
47+
});
48+
49+
it('resolves relative paths against project root', () => {
50+
expect(parseAbsolutePath({ screenshotPath: `${PATH_VARIABLES.unixSystemRootPath}/some/path.png`, projectRoot }))
51+
.toBe('/some/path.png');
52+
});
53+
});

src/afterScreenshot.hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const getConfigVariableOrThrow = <K extends keyof Cypress.PluginConfigOptions>(
2525
};
2626
/* c8 ignore stop */
2727

28-
const parseAbsolutePath = ({
28+
export const parseAbsolutePath = ({
2929
screenshotPath,
3030
projectRoot,
3131
}: {

src/task.hook.test.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,48 @@ const writeTmpFixture = async (pathToWriteTo: string, fixtureName: string) =>
2222
);
2323

2424
describe("getScreenshotPathTask", () => {
25+
const specPath = "some/nested/spec-path/spec.ts";
2526
it("returns sanitized path", () => {
2627
expect(
2728
getScreenshotPathTask({
2829
title: "some-title-withśpęćiał人物",
29-
imagesDir: "nested/images/dir",
30-
specPath: "some/nested/spec-path/spec.ts",
30+
imagesPath: "nested/images/dir",
31+
specPath,
3132
})
3233
).toBe(
33-
"__cp-visual-regression-diff_snapshots__/some/nested/spec-path/nested/images/dir/some-title-withśpęćiał人物.actual.png"
34+
"__cp-visual-regression-diff_snapshots__/nested/images/dir/some-title-withśpęćiał人物.actual.png"
3435
);
3536
});
37+
38+
it("supports {spec_path} variable", () => {
39+
expect(
40+
getScreenshotPathTask({
41+
title: "some-title",
42+
imagesPath: "{spec_path}/images/dir",
43+
specPath,
44+
})
45+
).toBe(
46+
"__cp-visual-regression-diff_snapshots__/some/nested/spec-path/images/dir/some-title.actual.png"
47+
);
48+
});
49+
50+
it("supports OS-specific absolute paths", () => {
51+
expect(
52+
getScreenshotPathTask({
53+
title: "some-title",
54+
imagesPath: "/images/dir",
55+
specPath,
56+
})
57+
).toBe("__cp-visual-regression-diff_snapshots__/{unix_system_root_path}/images/dir/some-title.actual.png");
58+
59+
expect(
60+
getScreenshotPathTask({
61+
title: "some-title",
62+
imagesPath: "C:/images/dir",
63+
specPath,
64+
})
65+
).toBe("__cp-visual-regression-diff_snapshots__/{win_system_root_path}/C/images/dir/some-title.actual.png");
66+
});
3667
});
3768

3869
describe("approveImageTask", () => {

0 commit comments

Comments
 (0)