@@ -10,25 +10,13 @@ declare global {
10
10
screenshotConfig ?: Partial < Cypress . ScreenshotDefaultsOptions > ;
11
11
diffConfig ?: Parameters < typeof pixelmatch > [ 5 ] ;
12
12
updateImages ?: boolean ;
13
+ /**
14
+ * @deprecated since version 3.0, use imagesPath instead
15
+ */
13
16
imagesDir ?: string ;
17
+ imagesPath ?: string ;
14
18
maxDiffThreshold ?: number ;
15
19
title ?: string ;
16
- matchAgainstPath ?: string ;
17
- // IDEA: to be implemented if support for files NOT from filesystem needed
18
- // matchAgainst?: string | Buffer;
19
- } ;
20
-
21
- type MatchImageReturn = {
22
- diffValue : number | undefined ;
23
- imgNewPath : string ;
24
- imgPath : string ;
25
- imgDiffPath : string ;
26
- imgNewBase64 : string | undefined ;
27
- imgBase64 : string | undefined ;
28
- imgDiffBase64 : string | undefined ;
29
- imgNew : InstanceType < Cypress [ 'Buffer' ] > | undefined ;
30
- img : InstanceType < Cypress [ 'Buffer' ] > | undefined ;
31
- imgDiff : InstanceType < Cypress [ 'Buffer' ] > | undefined ;
32
20
} ;
33
21
34
22
interface Chainable < Subject > {
@@ -37,11 +25,13 @@ declare global {
37
25
* @memberof Cypress.Chainable
38
26
* @example cy.get('.my-element').matchImage();
39
27
*/
40
- matchImage ( options ?: Cypress . MatchImageOptions ) : Chainable < MatchImageReturn > ;
28
+ matchImage ( options ?: Cypress . MatchImageOptions ) : Chainable < Subject > ;
41
29
}
42
30
}
43
31
}
44
32
33
+ const nameCacheCounter : Record < string , number > = { } ;
34
+
45
35
const constructCypressError = ( log : Cypress . Log , err : Error ) => {
46
36
// only way to throw & log the message properly in Cypress
47
37
// https://github.com/cypress-io/cypress/blob/5f94cad3cb4126e0567290b957050c33e3a78e3c/packages/driver/src/cypress/error_utils.ts#L214-L216
@@ -50,77 +40,96 @@ const constructCypressError = (log: Cypress.Log, err: Error) => {
50
40
return err ;
51
41
} ;
52
42
53
- export const getConfig = ( options : Cypress . MatchImageOptions ) => ( {
54
- scaleFactor :
55
- Cypress . env ( "pluginVisualRegressionForceDeviceScaleFactor" ) === false
56
- ? 1
57
- : 1 / window . devicePixelRatio ,
58
- updateImages :
59
- options . updateImages ||
60
- ( Cypress . env ( "pluginVisualRegressionUpdateImages" ) as
61
- | boolean
62
- | undefined ) ||
63
- false ,
64
- imagesDir :
43
+ const getImagesDir = ( options : Cypress . MatchImageOptions ) => {
44
+ const imagesDir =
65
45
options . imagesDir ||
66
- ( Cypress . env ( "pluginVisualRegressionImagesDir" ) as string | undefined ) ||
67
- "__image_snapshots__" ,
68
- maxDiffThreshold :
69
- options . maxDiffThreshold ||
70
- ( Cypress . env ( "pluginVisualRegressionMaxDiffThreshold" ) as
71
- | number
72
- | undefined ) ||
73
- 0.01 ,
74
- diffConfig :
75
- options . diffConfig ||
76
- ( Cypress . env ( "pluginVisualRegressionDiffConfig" ) as
77
- | Parameters < typeof pixelmatch > [ 5 ]
78
- | undefined ) ||
79
- { } ,
80
- screenshotConfig :
81
- options . screenshotConfig ||
82
- ( Cypress . env ( "pluginVisualRegressionScreenshotConfig" ) as
83
- | Partial < Cypress . ScreenshotDefaultsOptions >
84
- | undefined ) ||
85
- { } ,
86
- matchAgainstPath : options . matchAgainstPath || undefined ,
87
- } ) ;
46
+ ( Cypress . env ( "pluginVisualRegressionImagesDir" ) as string | undefined ) ;
47
+
48
+ // TODO: remove in 4.0.0
49
+ if ( imagesDir ) {
50
+ console . warn (
51
+ "@frsource/cypress-plugin-visual-regression-diff] `imagesDir` option is deprecated, use `imagesPath` instead (https://github.com/FRSOURCE/cypress-plugin-visual-regression-diff#configuration)"
52
+ ) ;
53
+ }
54
+
55
+ return imagesDir ;
56
+ } ;
57
+
58
+ export const getConfig = ( options : Cypress . MatchImageOptions ) => {
59
+ const imagesDir = getImagesDir ( options ) ;
60
+
61
+ return {
62
+ scaleFactor :
63
+ Cypress . env ( "pluginVisualRegressionForceDeviceScaleFactor" ) === false
64
+ ? 1
65
+ : 1 / window . devicePixelRatio ,
66
+ updateImages :
67
+ options . updateImages ||
68
+ ( Cypress . env ( "pluginVisualRegressionUpdateImages" ) as
69
+ | boolean
70
+ | undefined ) ||
71
+ false ,
72
+ imagesPath :
73
+ ( imagesDir && `{spec_path}/${ imagesDir } ` ) ||
74
+ options . imagesPath ||
75
+ ( Cypress . env ( "pluginVisualRegressionImagesPath" ) as string | undefined ) ||
76
+ "{spec_path}/__image_snapshots__" ,
77
+ maxDiffThreshold :
78
+ options . maxDiffThreshold ||
79
+ ( Cypress . env ( "pluginVisualRegressionMaxDiffThreshold" ) as
80
+ | number
81
+ | undefined ) ||
82
+ 0.01 ,
83
+ diffConfig :
84
+ options . diffConfig ||
85
+ ( Cypress . env ( "pluginVisualRegressionDiffConfig" ) as
86
+ | Parameters < typeof pixelmatch > [ 5 ]
87
+ | undefined ) ||
88
+ { } ,
89
+ screenshotConfig :
90
+ options . screenshotConfig ||
91
+ ( Cypress . env ( "pluginVisualRegressionScreenshotConfig" ) as
92
+ | Partial < Cypress . ScreenshotDefaultsOptions >
93
+ | undefined ) ||
94
+ { } ,
95
+ } ;
96
+ } ;
88
97
89
98
Cypress . Commands . add (
90
99
"matchImage" ,
91
100
{ prevSubject : "optional" } ,
92
101
( subject , options = { } ) => {
93
102
const $el = subject as JQuery < HTMLElement > | undefined ;
94
- let title : string ;
103
+ let title = options . title || Cypress . currentTest . titlePath . join ( " " ) ;
104
+ if ( typeof nameCacheCounter [ title ] === "undefined" )
105
+ nameCacheCounter [ title ] = - 1 ;
106
+ title += ` #${ ++ nameCacheCounter [ title ] } ` ;
95
107
96
108
const {
97
109
scaleFactor,
98
110
updateImages,
99
- imagesDir ,
111
+ imagesPath ,
100
112
maxDiffThreshold,
101
113
diffConfig,
102
114
screenshotConfig,
103
- matchAgainstPath,
104
115
} = getConfig ( options ) ;
105
116
106
117
return cy
107
118
. then ( ( ) =>
108
- cy . task < { screenshotPath : string ; title : string } > (
109
- TASK . getScreenshotPathInfo ,
119
+ cy . task < string > (
120
+ TASK . getScreenshotPath ,
110
121
{
111
- titleFromOptions :
112
- options . title || Cypress . currentTest . titlePath . join ( " " ) ,
113
- imagesDir,
122
+ title,
123
+ imagesPath,
114
124
specPath : Cypress . spec . relative ,
115
125
} ,
116
126
{ log : false }
117
127
)
118
128
)
119
- . then ( ( { screenshotPath, title : titleFromTask } ) => {
120
- title = titleFromTask ;
129
+ . then ( ( screenshotPath ) => {
121
130
let imgPath : string ;
122
131
return ( $el ? cy . wrap ( $el ) : cy )
123
- . screenshot ( screenshotPath as string , {
132
+ . screenshot ( screenshotPath , {
124
133
...screenshotConfig ,
125
134
onAfterScreenshot ( el , props ) {
126
135
imgPath = props . path ;
@@ -130,14 +139,14 @@ Cypress.Commands.add(
130
139
} )
131
140
. then ( ( ) => imgPath ) ;
132
141
} )
133
- . then ( ( imgPath ) => {
134
- return cy
142
+ . then ( ( imgPath ) =>
143
+ cy
135
144
. task < CompareImagesTaskReturn > (
136
145
TASK . compareImages ,
137
146
{
138
147
scaleFactor,
139
148
imgNew : imgPath ,
140
- imgOld : matchAgainstPath || imgPath . replace ( FILE_SUFFIX . actual , "" ) ,
149
+ imgOld : imgPath . replace ( FILE_SUFFIX . actual , "" ) ,
141
150
updateImages,
142
151
maxDiffThreshold,
143
152
diffConfig,
@@ -148,7 +157,7 @@ Cypress.Commands.add(
148
157
res,
149
158
imgPath,
150
159
} ) )
151
- } )
160
+ )
152
161
. then ( ( { res, imgPath } ) => {
153
162
const log = Cypress . log ( {
154
163
name : "log" ,
@@ -185,19 +194,6 @@ Cypress.Commands.add(
185
194
log . set ( "consoleProps" , ( ) => res ) ;
186
195
throw constructCypressError ( log , new Error ( res . message ) ) ;
187
196
}
188
-
189
- return {
190
- diffValue : res . imgDiff ,
191
- imgNewPath : imgPath ,
192
- imgPath : imgPath . replace ( FILE_SUFFIX . actual , "" ) ,
193
- imgDiffPath : imgPath . replace ( FILE_SUFFIX . actual , FILE_SUFFIX . diff ) ,
194
- imgNewBase64 : res . imgNewBase64 ,
195
- imgBase64 : res . imgOldBase64 ,
196
- imgDiffBase64 : res . imgDiffBase64 ,
197
- imgNew : typeof res . imgNewBase64 === 'string' ? Cypress . Buffer . from ( res . imgNewBase64 , 'base64' ) : undefined ,
198
- img : typeof res . imgOldBase64 === 'string' ? Cypress . Buffer . from ( res . imgOldBase64 , 'base64' ) : undefined ,
199
- imgDiff : typeof res . imgDiffBase64 === 'string' ? Cypress . Buffer . from ( res . imgDiffBase64 , 'base64' ) : undefined ,
200
- }
201
197
} ) ;
202
198
}
203
199
) ;
0 commit comments