Skip to content

Commit 77ae00c

Browse files
committed
fix: createMissingImages support
Signed-off-by: Jakub Freisler <[email protected]>
1 parent 36f2f0d commit 77ae00c

File tree

1 file changed

+17
-32
lines changed
  • packages/cypress-plugin-visual-regression-diff/src

1 file changed

+17
-32
lines changed

packages/cypress-plugin-visual-regression-diff/src/commands.ts

+17-32
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ const constructCypressError = (log: Cypress.Log, err: Error) => {
5959
return err;
6060
};
6161

62+
const capitalize = (text: string) =>
63+
text.charAt(0).toUpperCase() + text.slice(1);
64+
65+
const getPluginEnv = <R extends keyof Cypress.MatchImageOptions>(key: R) =>
66+
Cypress.env(`pluginVisualRegression${capitalize(key)}`) as
67+
| Cypress.MatchImageOptions[R]
68+
| undefined;
69+
6270
const getImagesDir = (options: Cypress.MatchImageOptions) => {
63-
const imagesDir =
64-
options.imagesDir ||
65-
(Cypress.env("pluginVisualRegressionImagesDir") as string | undefined);
71+
const imagesDir = options.imagesDir || getPluginEnv("imagesDir");
6672

6773
// TODO: remove in 4.0.0
6874
if (imagesDir) {
@@ -76,49 +82,28 @@ const getImagesDir = (options: Cypress.MatchImageOptions) => {
7682

7783
export const getConfig = (options: Cypress.MatchImageOptions) => {
7884
const imagesDir = getImagesDir(options);
79-
const maxDiffThreshold =
80-
options.maxDiffThreshold ??
81-
(Cypress.env("pluginVisualRegressionMaxDiffThreshold") as
82-
| number
83-
| undefined);
8485

8586
return {
8687
scaleFactor:
8788
options.forceDeviceScaleFactor === false ||
88-
Cypress.env("pluginVisualRegressionForceDeviceScaleFactor") === false
89+
getPluginEnv("forceDeviceScaleFactor") === false
8990
? 1
9091
: 1 / window.devicePixelRatio,
9192
createMissingImages:
92-
options.createMissingImages ||
93-
(Cypress.env("pluginVisualRegressionCreateMissingImages") as
94-
| boolean
95-
| undefined) ||
93+
options.createMissingImages ??
94+
getPluginEnv("createMissingImages") ??
9695
true,
97-
updateImages:
98-
options.updateImages ||
99-
(Cypress.env("pluginVisualRegressionUpdateImages") as
100-
| boolean
101-
| undefined) ||
102-
false,
96+
updateImages: options.updateImages ?? getPluginEnv("updateImages") ?? false,
10397
imagesPath:
10498
(imagesDir && `{spec_path}/${imagesDir}`) ||
10599
options.imagesPath ||
106-
(Cypress.env("pluginVisualRegressionImagesPath") as string | undefined) ||
100+
getPluginEnv("imagesPath") ||
107101
"{spec_path}/__image_snapshots__",
108102
maxDiffThreshold:
109-
typeof maxDiffThreshold === "number" ? maxDiffThreshold : 0.01,
110-
diffConfig:
111-
options.diffConfig ||
112-
(Cypress.env("pluginVisualRegressionDiffConfig") as
113-
| Parameters<typeof pixelmatch>[5]
114-
| undefined) ||
115-
{},
103+
options.maxDiffThreshold ?? getPluginEnv("maxDiffThreshold") ?? 0.01,
104+
diffConfig: options.diffConfig || getPluginEnv("diffConfig") || {},
116105
screenshotConfig:
117-
options.screenshotConfig ||
118-
(Cypress.env("pluginVisualRegressionScreenshotConfig") as
119-
| Partial<Cypress.ScreenshotDefaultsOptions>
120-
| undefined) ||
121-
{},
106+
options.screenshotConfig || getPluginEnv("screenshotConfig") || {},
122107
matchAgainstPath: options.matchAgainstPath || undefined,
123108
};
124109
};

0 commit comments

Comments
 (0)