Skip to content

Commit 866860e

Browse files
Print eventual lightning CSS parsing errors when the CSS matcher fail (#14034)
1 parent bdc87ae commit 866860e

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

jest/customMatchers.js

+41-32
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,42 @@ function formatPrettier(input) {
2323

2424
function format(input) {
2525
try {
26-
return lightningcss
27-
.transform({
28-
filename: 'input.css',
29-
code: Buffer.from(input),
30-
minify: false,
31-
targets: { chrome: 106 << 16 },
32-
drafts: {
33-
nesting: true,
34-
customMedia: true,
35-
},
36-
})
37-
.code.toString('utf8')
26+
return [
27+
lightningcss
28+
.transform({
29+
filename: 'input.css',
30+
code: Buffer.from(input),
31+
minify: false,
32+
targets: { chrome: 106 << 16 },
33+
drafts: {
34+
nesting: true,
35+
customMedia: true,
36+
},
37+
})
38+
.code.toString('utf8'),
39+
null,
40+
]
3841
} catch (err) {
42+
let lines = err.source.split('\n')
43+
let e = new Error(
44+
[
45+
'Error formatting using Lightning CSS:',
46+
'',
47+
...[
48+
'```css',
49+
...lines.slice(Math.max(err.loc.line - 3, 0), err.loc.line),
50+
' '.repeat(err.loc.column - 1) + '^-- ' + err.toString(),
51+
...lines.slice(err.loc.line, err.loc.line + 2),
52+
'```',
53+
],
54+
].join('\n')
55+
)
3956
try {
4057
// Lightning CSS is pretty strict, so it will fail for `@media screen(md) {}` for example,
4158
// in that case we can fallback to prettier since it doesn't really care. However if an
4259
// actual syntax error is made, then we still want to show the proper error.
43-
return formatPrettier(input.replace(/\n/g, ''))
60+
return [formatPrettier(input.replace(/\n/g, '')), e]
4461
} catch {
45-
let lines = err.source.split('\n')
46-
let e = new Error(
47-
[
48-
'Error formatting using Lightning CSS:',
49-
'',
50-
...[
51-
'```css',
52-
...lines.slice(Math.max(err.loc.line - 3, 0), err.loc.line),
53-
' '.repeat(err.loc.column - 1) + '^-- ' + err.toString(),
54-
...lines.slice(err.loc.line, err.loc.line + 2),
55-
'```',
56-
],
57-
].join('\n')
58-
)
5962
if (Error.captureStackTrace) {
6063
Error.captureStackTrace(e, toMatchFormattedCss)
6164
}
@@ -71,8 +74,8 @@ function toMatchFormattedCss(received = '', argument = '') {
7174
promise: this.promise,
7275
}
7376

74-
let formattedReceived = format(received)
75-
let formattedArgument = format(argument)
77+
let [formattedReceived, formattingReceivedError] = format(received)
78+
let [formattedArgument, formattingArgumentError] = format(argument)
7679

7780
let pass = formattedReceived === formattedArgument
7881

@@ -99,7 +102,9 @@ function toMatchFormattedCss(received = '', argument = '') {
99102
(diffString && diffString.includes('- Expect')
100103
? `Difference:\n\n${diffString}`
101104
: `Expected: ${this.utils.printExpected(expected)}\n` +
102-
`Received: ${this.utils.printReceived(actual)}`)
105+
`Received: ${this.utils.printReceived(actual)}`) +
106+
(formattingReceivedError ? '\n\n' + formattingReceivedError : '') +
107+
(formattingArgumentError ? '\n\n' + formattingArgumentError : '')
103108
)
104109
}
105110

@@ -118,7 +123,9 @@ expect.extend({
118123
promise: this.promise,
119124
}
120125

121-
let pass = format(received).includes(format(argument))
126+
let [formattedReceived, formattedReceivedError] = format(received)
127+
let [formattedArgument, formattedArgumentError] = format(argument)
128+
let pass = formattedReceived.includes(formattedArgument)
122129

123130
let message = pass
124131
? () => {
@@ -143,7 +150,9 @@ expect.extend({
143150
(diffString && diffString.includes('- Expect')
144151
? `Difference:\n\n${diffString}`
145152
: `Expected: ${this.utils.printExpected(expected)}\n` +
146-
`Received: ${this.utils.printReceived(actual)}`)
153+
`Received: ${this.utils.printReceived(actual)}`) +
154+
(formattedReceivedError ? '\n\n' + formattedReceivedError : '') +
155+
(formattedArgumentError ? '\n\n' + formattedArgumentError : '')
147156
)
148157
}
149158

0 commit comments

Comments
 (0)