1
1
import path from 'path' ;
2
2
3
- import { VisualRegressionPluginOptions } from './config' ;
3
+ import { VisualRegressionPluginOptions , DiffResult } from './config' ;
4
4
import { VisualRegressionError } from './VisualRegressionError' ;
5
5
6
6
function resolveImagePath ( baseDir : string , name : string ) {
@@ -22,6 +22,30 @@ export interface VisualDiffCommandContext {
22
22
testFile : string ;
23
23
}
24
24
25
+ function passesFailureThreshold (
26
+ { diffPercentage, diffPixels } : DiffResult ,
27
+ { failureThresholdType, failureThreshold } : VisualRegressionPluginOptions ,
28
+ ) : { passed : boolean ; message ?: string } {
29
+ if ( failureThresholdType === 'percent' ) {
30
+ return diffPercentage <= failureThreshold
31
+ ? { passed : true }
32
+ : {
33
+ passed : false ,
34
+ // if diff is suitably small, output raw value, otherwise to two decimal points.
35
+ // this avoids outputting a failure value of "0.00%"
36
+ message : `${ diffPercentage < 0.005 ? diffPercentage : diffPercentage . toFixed ( 2 ) } %` ,
37
+ } ;
38
+ }
39
+
40
+ if ( failureThresholdType === 'pixel' ) {
41
+ return diffPixels <= failureThreshold
42
+ ? { passed : true }
43
+ : { passed : false , message : `${ diffPixels } pixels` } ;
44
+ }
45
+
46
+ throw new VisualRegressionError ( `Unrecognized failureThresholdType: ${ failureThresholdType } ` ) ;
47
+ }
48
+
25
49
export async function visualDiffCommand (
26
50
options : VisualRegressionPluginOptions ,
27
51
image : Buffer ,
@@ -79,22 +103,28 @@ export async function visualDiffCommand(
79
103
} ;
80
104
}
81
105
82
- const { diffImage , diffPercentage , error } = await options . getImageDiff ( {
106
+ const result = await options . getImageDiff ( {
83
107
name,
84
108
baselineImage,
85
109
image,
86
110
options : options . diffOptions ,
87
111
} ) ;
88
112
113
+ const { error, diffImage } = result ;
114
+
89
115
if ( error ) {
90
116
// The diff has failed, be sure to save the new image.
91
117
await saveFailed ( ) ;
92
118
await saveDiff ( ) ;
93
119
94
- throw new VisualRegressionError ( error ) ;
120
+ return {
121
+ passed : false ,
122
+ errorMessage : error ,
123
+ diffPercentage : - 1 ,
124
+ } ;
95
125
}
96
126
97
- const passed = diffPercentage === 0 ;
127
+ const { passed, message } = passesFailureThreshold ( result , options ) ;
98
128
99
129
if ( ! passed ) {
100
130
await saveDiff ( ) ;
@@ -104,14 +134,9 @@ export async function visualDiffCommand(
104
134
await saveFailed ( ) ;
105
135
}
106
136
107
- // if diff is suitably small, output raw value, otherwise to two decimal points.
108
- // this avoids outputting a message like "New screenshot is 0.00% different"
109
- const diffPercentageToDisplay =
110
- diffPercentage < 0.005 ? diffPercentage : diffPercentage . toFixed ( 2 ) ;
111
-
112
137
return {
113
138
errorMessage : ! passed
114
- ? `Visual diff failed. New screenshot is ${ diffPercentageToDisplay } % different.\nSee diff for details: ${ diffFilePath } `
139
+ ? `Visual diff failed. New screenshot is ${ message } different.\nSee diff for details: ${ diffFilePath } `
115
140
: undefined ,
116
141
diffPercentage : - 1 ,
117
142
passed,
0 commit comments