@@ -9,6 +9,16 @@ import sanitize from "sanitize-filename";
9
9
10
10
type NotFalsy < T > = T extends false | null | undefined ? never : T ;
11
11
12
+ type CompareImagesCfg = {
13
+ scaleFactor : number ;
14
+ title : string ;
15
+ imgNew : string ;
16
+ imgOld : string ;
17
+ updateImages : boolean ;
18
+ maxDiffThreshold : number ;
19
+ diffConfig : Parameters < typeof pixelmatch > [ 5 ] ;
20
+ } & Parameters < typeof pixelmatch > [ 5 ] ;
21
+
12
22
const round = ( n : number ) => Math . ceil ( n * 1000 ) / 1000 ;
13
23
14
24
const createImageResizer = ( width : number , height : number ) => ( source : PNG ) => {
@@ -17,11 +27,13 @@ const createImageResizer = (width: number, height: number) => (source: PNG) => {
17
27
return resized ;
18
28
} ;
19
29
30
+ const inArea = ( x : number , y : number , height : number , width : number ) =>
31
+ y > height || x > width ;
32
+
20
33
const fillSizeDifference = ( width : number , height : number ) => ( image : PNG ) => {
21
- const inArea = ( x : number , y : number ) => y > height || x > width ;
22
34
for ( let y = 0 ; y < image . height ; y ++ ) {
23
35
for ( let x = 0 ; x < image . width ; x ++ ) {
24
- if ( inArea ( x , y ) ) {
36
+ if ( inArea ( x , y , height , width ) ) {
25
37
const idx = ( image . width * y + x ) << 2 ;
26
38
image . data [ idx ] = 0 ;
27
39
image . data [ idx + 1 ] = 0 ;
@@ -81,25 +93,22 @@ const getConfigVariableOrThrow = <K extends keyof Cypress.PluginConfigOptions>(
81
93
throw `[Image snapshot] CypressConfig.${ name } cannot be missing or \`false\`!` ;
82
94
} ;
83
95
84
- export const initPlugin = (
85
- on : Cypress . PluginEvents ,
86
- config : Cypress . PluginConfigOptions
87
- ) => {
88
- if ( config . env [ "pluginVisualRegressionForceDeviceScaleFactor" ] !== false ) {
89
- // based on https://github.com/cypress-io/cypress/issues/2102#issuecomment-521299946
90
- on ( "before:browser:launch" , ( browser , launchOptions ) => {
91
- if ( browser . name === "chrome" || browser . name === "chromium" ) {
92
- launchOptions . args . push ( "--force-device-scale-factor=1" ) ;
93
- launchOptions . args . push ( "--high-dpi-support=1" ) ;
94
- } else if ( browser . name === "electron" && browser . isHeaded ) {
95
- // eslint-disable-next-line no-console
96
- console . log (
97
- "There isn't currently a way of setting the device scale factor in Cypress when running headed electron so we disable the image regression commands."
98
- ) ;
99
- }
100
- } ) ;
101
- }
96
+ const initForceDeviceScaleFactor = ( on : Cypress . PluginEvents ) => {
97
+ // based on https://github.com/cypress-io/cypress/issues/2102#issuecomment-521299946
98
+ on ( "before:browser:launch" , ( browser , launchOptions ) => {
99
+ if ( browser . name === "chrome" || browser . name === "chromium" ) {
100
+ launchOptions . args . push ( "--force-device-scale-factor=1" ) ;
101
+ launchOptions . args . push ( "--high-dpi-support=1" ) ;
102
+ } else if ( browser . name === "electron" && browser . isHeaded ) {
103
+ // eslint-disable-next-line no-console
104
+ console . log (
105
+ "There isn't currently a way of setting the device scale factor in Cypress when running headed electron so we disable the image regression commands."
106
+ ) ;
107
+ }
108
+ } ) ;
109
+ } ;
102
110
111
+ const initTaskHooks = ( on : Cypress . PluginEvents ) => {
103
112
on ( "task" , {
104
113
[ TASK . getScreenshotPath ] ( { title, imagesDir, specPath } ) {
105
114
return path . join (
@@ -123,17 +132,7 @@ export const initPlugin = (
123
132
124
133
return null ;
125
134
} ,
126
- async [ TASK . compareImages ] (
127
- cfg : {
128
- scaleFactor : number ;
129
- title : string ;
130
- imgNew : string ;
131
- imgOld : string ;
132
- updateImages : boolean ;
133
- maxDiffThreshold : number ;
134
- diffConfig : Parameters < typeof pixelmatch > [ 5 ] ;
135
- } & Parameters < typeof pixelmatch > [ 5 ]
136
- ) : Promise < null | {
135
+ async [ TASK . compareImages ] ( cfg : CompareImagesCfg ) : Promise < null | {
137
136
error ?: boolean ;
138
137
message ?: string ;
139
138
imgDiff ?: number ;
@@ -227,7 +226,12 @@ export const initPlugin = (
227
226
return null ;
228
227
} ,
229
228
} ) ;
229
+ } ;
230
230
231
+ const initAfterScreenshotHook = (
232
+ on : Cypress . PluginEvents ,
233
+ config : Cypress . PluginConfigOptions
234
+ ) => {
231
235
on ( "after:screenshot" , ( details ) => {
232
236
if ( details . name ?. indexOf ( IMAGE_SNAPSHOT_PREFIX ) !== 0 ) return ;
233
237
@@ -260,3 +264,15 @@ export const initPlugin = (
260
264
} ) ;
261
265
} ) ;
262
266
} ;
267
+
268
+ export const initPlugin = (
269
+ on : Cypress . PluginEvents ,
270
+ config : Cypress . PluginConfigOptions
271
+ ) => {
272
+ if ( config . env [ "pluginVisualRegressionForceDeviceScaleFactor" ] !== false ) {
273
+ initForceDeviceScaleFactor ( on ) ;
274
+ }
275
+
276
+ initTaskHooks ( on ) ;
277
+ initAfterScreenshotHook ( on , config ) ;
278
+ } ;
0 commit comments