@@ -59,10 +59,16 @@ const constructCypressError = (log: Cypress.Log, err: Error) => {
59
59
return err ;
60
60
} ;
61
61
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
+
62
70
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" ) ;
66
72
67
73
// TODO: remove in 4.0.0
68
74
if ( imagesDir ) {
@@ -76,49 +82,28 @@ const getImagesDir = (options: Cypress.MatchImageOptions) => {
76
82
77
83
export const getConfig = ( options : Cypress . MatchImageOptions ) => {
78
84
const imagesDir = getImagesDir ( options ) ;
79
- const maxDiffThreshold =
80
- options . maxDiffThreshold ??
81
- ( Cypress . env ( "pluginVisualRegressionMaxDiffThreshold" ) as
82
- | number
83
- | undefined ) ;
84
85
85
86
return {
86
87
scaleFactor :
87
88
options . forceDeviceScaleFactor === false ||
88
- Cypress . env ( "pluginVisualRegressionForceDeviceScaleFactor ") === false
89
+ getPluginEnv ( "forceDeviceScaleFactor ") === false
89
90
? 1
90
91
: 1 / window . devicePixelRatio ,
91
92
createMissingImages :
92
- options . createMissingImages ||
93
- ( Cypress . env ( "pluginVisualRegressionCreateMissingImages" ) as
94
- | boolean
95
- | undefined ) ||
93
+ options . createMissingImages ??
94
+ getPluginEnv ( "createMissingImages" ) ??
96
95
true ,
97
- updateImages :
98
- options . updateImages ||
99
- ( Cypress . env ( "pluginVisualRegressionUpdateImages" ) as
100
- | boolean
101
- | undefined ) ||
102
- false ,
96
+ updateImages : options . updateImages ?? getPluginEnv ( "updateImages" ) ?? false ,
103
97
imagesPath :
104
98
( imagesDir && `{spec_path}/${ imagesDir } ` ) ||
105
99
options . imagesPath ||
106
- ( Cypress . env ( "pluginVisualRegressionImagesPath" ) as string | undefined ) ||
100
+ getPluginEnv ( "imagesPath" ) ||
107
101
"{spec_path}/__image_snapshots__" ,
108
102
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" ) || { } ,
116
105
screenshotConfig :
117
- options . screenshotConfig ||
118
- ( Cypress . env ( "pluginVisualRegressionScreenshotConfig" ) as
119
- | Partial < Cypress . ScreenshotDefaultsOptions >
120
- | undefined ) ||
121
- { } ,
106
+ options . screenshotConfig || getPluginEnv ( "screenshotConfig" ) || { } ,
122
107
matchAgainstPath : options . matchAgainstPath || undefined ,
123
108
} ;
124
109
} ;
0 commit comments