Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions jest/customMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,42 @@ function formatPrettier(input) {

function format(input) {
try {
return lightningcss
.transform({
filename: 'input.css',
code: Buffer.from(input),
minify: false,
targets: { chrome: 106 << 16 },
drafts: {
nesting: true,
customMedia: true,
},
})
.code.toString('utf8')
return [
lightningcss
.transform({
filename: 'input.css',
code: Buffer.from(input),
minify: false,
targets: { chrome: 106 << 16 },
drafts: {
nesting: true,
customMedia: true,
},
})
.code.toString('utf8'),
null,
]
} catch (err) {
let lines = err.source.split('\n')
let e = new Error(
[
'Error formatting using Lightning CSS:',
'',
...[
'```css',
...lines.slice(Math.max(err.loc.line - 3, 0), err.loc.line),
' '.repeat(err.loc.column - 1) + '^-- ' + err.toString(),
...lines.slice(err.loc.line, err.loc.line + 2),
'```',
],
].join('\n')
)
try {
// Lightning CSS is pretty strict, so it will fail for `@media screen(md) {}` for example,
// in that case we can fallback to prettier since it doesn't really care. However if an
// actual syntax error is made, then we still want to show the proper error.
return formatPrettier(input.replace(/\n/g, ''))
return [formatPrettier(input.replace(/\n/g, '')), e]
} catch {
let lines = err.source.split('\n')
let e = new Error(
[
'Error formatting using Lightning CSS:',
'',
...[
'```css',
...lines.slice(Math.max(err.loc.line - 3, 0), err.loc.line),
' '.repeat(err.loc.column - 1) + '^-- ' + err.toString(),
...lines.slice(err.loc.line, err.loc.line + 2),
'```',
],
].join('\n')
)
if (Error.captureStackTrace) {
Error.captureStackTrace(e, toMatchFormattedCss)
}
Expand All @@ -71,8 +74,8 @@ function toMatchFormattedCss(received = '', argument = '') {
promise: this.promise,
}

let formattedReceived = format(received)
let formattedArgument = format(argument)
let [formattedReceived, formattingReceivedError] = format(received)
let [formattedArgument, formattingArgumentError] = format(argument)

let pass = formattedReceived === formattedArgument

Expand All @@ -99,7 +102,9 @@ function toMatchFormattedCss(received = '', argument = '') {
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(actual)}`)
`Received: ${this.utils.printReceived(actual)}`) +
(formattingReceivedError ? '\n\n' + formattingReceivedError : '') +
(formattingArgumentError ? '\n\n' + formattingArgumentError : '')
)
}

Expand All @@ -118,7 +123,9 @@ expect.extend({
promise: this.promise,
}

let pass = format(received).includes(format(argument))
let [formattedReceived, formattedReceivedError] = format(received)
let [formattedArgument, formattedArgumentError] = format(argument)
let pass = formattedReceived.includes(formattedArgument)

let message = pass
? () => {
Expand All @@ -143,7 +150,9 @@ expect.extend({
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(actual)}`)
`Received: ${this.utils.printReceived(actual)}`) +
(formattedReceivedError ? '\n\n' + formattedReceivedError : '') +
(formattedArgumentError ? '\n\n' + formattedArgumentError : '')
)
}

Expand Down