Skip to content

Commit f7d63d1

Browse files
committed
Remove colours from strings during tests
1 parent 56d24b0 commit f7d63d1

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/format.test.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ const {
55
formatResults,
66
} = require('./format');
77

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+
};
17+
818
describe('format', () => {
919
const getCategories = ({ score }) => [
1020
{
@@ -42,13 +52,12 @@ describe('format', () => {
4252
},
4353
};
4454

45-
// Awkward formatting as the strings contain ANSI escape codes for colours.
4655
const formattedError = {
4756
details:
48-
" '\x1B[36mrobots.txt is valid\x1B[39m' received a score of \x1B[33m0\x1B[39m\n" +
49-
" '\x1B[36mTap targets are sized appropriately\x1B[39m' received a score of \x1B[33m0.5\x1B[39m",
57+
" 'robots.txt is valid' received a score of 0\n" +
58+
" 'Tap targets are sized appropriately' received a score of 0.5",
5059
message:
51-
'Expected category \x1B[36mPerformance\x1B[39m to be greater or equal to \x1B[32m1\x1B[39m but got \x1B[31m0.5\x1B[39m',
60+
'Expected category Performance to be greater or equal to 1 but got 0.5',
5261
};
5362

5463
it('returns an expected error message and list of details with valid score', () => {
@@ -58,7 +67,12 @@ describe('format', () => {
5867
getCategories({ score: 0.5 }),
5968
audits,
6069
);
61-
expect(errorMessage).toEqual(formattedError);
70+
expect(sanitizeString(errorMessage.details)).toEqual(
71+
formattedError.details,
72+
);
73+
expect(sanitizeString(errorMessage.message)).toEqual(
74+
formattedError.message,
75+
);
6276
});
6377

6478
describe('belowThreshold', () => {
@@ -92,8 +106,8 @@ describe('format', () => {
92106
audits,
93107
);
94108
// Matching is awkward as the strings contain ANSI escape codes for colours.
95-
expect(errorMessage.message).toContain(
96-
'to be greater or equal to \x1B[32m1\x1B[39m but got \x1B[31munknown\x1B[39m',
109+
expect(sanitizeString(errorMessage.message)).toContain(
110+
'to be greater or equal to 1 but got unknown',
97111
);
98112
});
99113
});
@@ -170,7 +184,12 @@ describe('format', () => {
170184
results: getResults(),
171185
thresholds,
172186
});
173-
expect(formattedResults.errors).toEqual([formattedError]);
187+
expect(sanitizeString(formattedResults.errors[0].message)).toEqual(
188+
formattedError.message,
189+
);
190+
expect(sanitizeString(formattedResults.errors[0].details)).toEqual(
191+
formattedError.details,
192+
);
174193
expect(formattedResults.summary).toEqual([
175194
{
176195
id: 'performance',

0 commit comments

Comments
 (0)