@@ -6,14 +6,11 @@ const {
6
6
} = require ( './format' ) ;
7
7
8
8
// Strip ANSI color codes from strings, as they make CI sad.
9
- const sanitizeString = ( str ) => {
10
- return str
11
- . replaceAll ( '\x1B[31m' , '' )
12
- . replaceAll ( '\x1B[32m' , '' )
13
- . replaceAll ( '\x1B[33m' , '' )
14
- . replaceAll ( '\x1B[36m' , '' )
15
- . replaceAll ( '\x1B[39m' , '' ) ;
16
- } ;
9
+ const stripAnsiCodes = ( str ) =>
10
+ str . replace (
11
+ / [ \u001b \u009b ] [ [ ( ) # ; ? ] * (?: [ 0 - 9 ] { 1 , 4 } (?: ; [ 0 - 9 ] { 0 , 4 } ) * ) ? [ 0 - 9 A - O R Z c f - n q r y = > < ] / g,
12
+ '' ,
13
+ ) ;
17
14
18
15
describe ( 'format' , ( ) => {
19
16
const getCategories = ( { score } ) => [
@@ -60,21 +57,6 @@ describe('format', () => {
60
57
'Expected category Performance to be greater or equal to 1 but got 0.5' ,
61
58
} ;
62
59
63
- it ( 'returns an expected error message and list of details with valid score' , ( ) => {
64
- const errorMessage = getError (
65
- 'performance' ,
66
- 1 ,
67
- getCategories ( { score : 0.5 } ) ,
68
- audits ,
69
- ) ;
70
- expect ( sanitizeString ( errorMessage . details ) ) . toEqual (
71
- formattedError . details ,
72
- ) ;
73
- expect ( sanitizeString ( errorMessage . message ) ) . toEqual (
74
- formattedError . message ,
75
- ) ;
76
- } ) ;
77
-
78
60
describe ( 'belowThreshold' , ( ) => {
79
61
const categories = [
80
62
{ title : 'Performance' , score : 0.9 , id : 'performance' } ,
@@ -98,15 +80,29 @@ describe('format', () => {
98
80
} ) ;
99
81
100
82
describe ( 'getError' , ( ) => {
83
+ it ( 'returns an expected error message and list of details with valid score' , ( ) => {
84
+ const errorMessage = getError (
85
+ 'performance' ,
86
+ 1 ,
87
+ getCategories ( { score : 0.5 } ) ,
88
+ audits ,
89
+ ) ;
90
+ expect ( stripAnsiCodes ( errorMessage . details ) ) . toEqual (
91
+ formattedError . details ,
92
+ ) ;
93
+ expect ( stripAnsiCodes ( errorMessage . message ) ) . toEqual (
94
+ formattedError . message ,
95
+ ) ;
96
+ } ) ;
97
+
101
98
it ( 'returns an expected error message and list of details without valid score' , ( ) => {
102
99
const errorMessage = getError (
103
100
'performance' ,
104
101
1 ,
105
102
getCategories ( { score : null } ) ,
106
103
audits ,
107
104
) ;
108
- // Matching is awkward as the strings contain ANSI escape codes for colours.
109
- expect ( sanitizeString ( errorMessage . message ) ) . toContain (
105
+ expect ( stripAnsiCodes ( errorMessage . message ) ) . toContain (
110
106
'to be greater or equal to 1 but got unknown' ,
111
107
) ;
112
108
} ) ;
@@ -184,10 +180,10 @@ describe('format', () => {
184
180
results : getResults ( ) ,
185
181
thresholds,
186
182
} ) ;
187
- expect ( sanitizeString ( formattedResults . errors [ 0 ] . message ) ) . toEqual (
183
+ expect ( stripAnsiCodes ( formattedResults . errors [ 0 ] . message ) ) . toEqual (
188
184
formattedError . message ,
189
185
) ;
190
- expect ( sanitizeString ( formattedResults . errors [ 0 ] . details ) ) . toEqual (
186
+ expect ( stripAnsiCodes ( formattedResults . errors [ 0 ] . details ) ) . toEqual (
191
187
formattedError . details ,
192
188
) ;
193
189
expect ( formattedResults . summary ) . toEqual ( [
0 commit comments