@@ -62,11 +62,27 @@ const constructCypressError = (log: Cypress.Log, err: Error) => {
62
62
const capitalize = ( text : string ) =>
63
63
text . charAt ( 0 ) . toUpperCase ( ) + text . slice ( 1 ) ;
64
64
65
- const getPluginEnv = < R extends keyof Cypress . MatchImageOptions > ( key : R ) =>
65
+ const getPluginEnv = < K extends keyof Cypress . MatchImageOptions > ( key : K ) =>
66
66
Cypress . env ( `pluginVisualRegression${ capitalize ( key ) } ` ) as
67
- | Cypress . MatchImageOptions [ R ]
67
+ | Cypress . MatchImageOptions [ K ]
68
68
| undefined ;
69
69
70
+ const booleanOption = < K extends keyof Cypress . MatchImageOptions , Return > (
71
+ options : Cypress . MatchImageOptions ,
72
+ key : K ,
73
+ truthyValue : Return ,
74
+ falsyValue : Return
75
+ ) =>
76
+ options [ key ] === false || getPluginEnv ( key ) === false
77
+ ? truthyValue
78
+ : falsyValue ;
79
+
80
+ const optionWithDefaults = < K extends keyof Cypress . MatchImageOptions > (
81
+ options : Cypress . MatchImageOptions ,
82
+ key : K ,
83
+ defaultValue : NonNullable < Cypress . MatchImageOptions [ K ] >
84
+ ) => options [ key ] ?? getPluginEnv ( key ) ?? defaultValue ;
85
+
70
86
const getImagesDir = ( options : Cypress . MatchImageOptions ) => {
71
87
const imagesDir = options . imagesDir || getPluginEnv ( "imagesDir" ) ;
72
88
@@ -80,34 +96,31 @@ const getImagesDir = (options: Cypress.MatchImageOptions) => {
80
96
return imagesDir ;
81
97
} ;
82
98
83
- export const getConfig = ( options : Cypress . MatchImageOptions ) => {
99
+ const getImagesPath = ( options : Cypress . MatchImageOptions ) => {
84
100
const imagesDir = getImagesDir ( options ) ;
85
101
86
- return {
87
- scaleFactor :
88
- options . forceDeviceScaleFactor === false ||
89
- getPluginEnv ( "forceDeviceScaleFactor" ) === false
90
- ? 1
91
- : 1 / window . devicePixelRatio ,
92
- createMissingImages :
93
- options . createMissingImages ??
94
- getPluginEnv ( "createMissingImages" ) ??
95
- true ,
96
- updateImages : options . updateImages ?? getPluginEnv ( "updateImages" ) ?? false ,
97
- imagesPath :
98
- ( imagesDir && `{spec_path}/${ imagesDir } ` ) ||
99
- options . imagesPath ||
100
- getPluginEnv ( "imagesPath" ) ||
101
- "{spec_path}/__image_snapshots__" ,
102
- maxDiffThreshold :
103
- options . maxDiffThreshold ?? getPluginEnv ( "maxDiffThreshold" ) ?? 0.01 ,
104
- diffConfig : options . diffConfig || getPluginEnv ( "diffConfig" ) || { } ,
105
- screenshotConfig :
106
- options . screenshotConfig || getPluginEnv ( "screenshotConfig" ) || { } ,
107
- matchAgainstPath : options . matchAgainstPath || undefined ,
108
- } ;
102
+ return (
103
+ ( imagesDir && `{spec_path}/${ imagesDir } ` ) ||
104
+ optionWithDefaults ( options , "imagesPath" , "{spec_path}/__image_snapshots__" )
105
+ ) ;
109
106
} ;
110
107
108
+ export const getConfig = ( options : Cypress . MatchImageOptions ) => ( {
109
+ scaleFactor : booleanOption (
110
+ options ,
111
+ "forceDeviceScaleFactor" ,
112
+ 1 ,
113
+ 1 / window . devicePixelRatio
114
+ ) ,
115
+ createMissingImages : optionWithDefaults ( options , "createMissingImages" , true ) ,
116
+ updateImages : optionWithDefaults ( options , "updateImages" , false ) ,
117
+ imagesPath : getImagesPath ( options ) ,
118
+ maxDiffThreshold : optionWithDefaults ( options , "maxDiffThreshold" , 0.01 ) ,
119
+ diffConfig : optionWithDefaults ( options , "diffConfig" , { } ) ,
120
+ screenshotConfig : optionWithDefaults ( options , "screenshotConfig" , { } ) ,
121
+ matchAgainstPath : options . matchAgainstPath || undefined ,
122
+ } ) ;
123
+
111
124
Cypress . Commands . add (
112
125
"matchImage" ,
113
126
{ prevSubject : "optional" } ,
0 commit comments