Skip to content

Commit dbd8f9a

Browse files
committed
Now with added node 14 support
1 parent f7d63d1 commit dbd8f9a

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/format.test.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ const {
66
} = require('./format');
77

88
// 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-9A-ORZcf-nqry=><]/g,
12+
'',
13+
);
1714

1815
describe('format', () => {
1916
const getCategories = ({ score }) => [
@@ -60,21 +57,6 @@ describe('format', () => {
6057
'Expected category Performance to be greater or equal to 1 but got 0.5',
6158
};
6259

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-
7860
describe('belowThreshold', () => {
7961
const categories = [
8062
{ title: 'Performance', score: 0.9, id: 'performance' },
@@ -98,15 +80,29 @@ describe('format', () => {
9880
});
9981

10082
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+
10198
it('returns an expected error message and list of details without valid score', () => {
10299
const errorMessage = getError(
103100
'performance',
104101
1,
105102
getCategories({ score: null }),
106103
audits,
107104
);
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(
110106
'to be greater or equal to 1 but got unknown',
111107
);
112108
});
@@ -184,10 +180,10 @@ describe('format', () => {
184180
results: getResults(),
185181
thresholds,
186182
});
187-
expect(sanitizeString(formattedResults.errors[0].message)).toEqual(
183+
expect(stripAnsiCodes(formattedResults.errors[0].message)).toEqual(
188184
formattedError.message,
189185
);
190-
expect(sanitizeString(formattedResults.errors[0].details)).toEqual(
186+
expect(stripAnsiCodes(formattedResults.errors[0].details)).toEqual(
191187
formattedError.details,
192188
);
193189
expect(formattedResults.summary).toEqual([

0 commit comments

Comments
 (0)