tailwindcss/jest/customMatchers.js

179 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2021-04-02 13:51:48 -04:00
const prettier = require('prettier')
const { diff } = require('jest-diff')
const log = require('../src/util/log').default
const { version } = require('../package.json')
function license() {
return `/* ! tailwindcss v${version} | MIT License | https://tailwindcss.com */\n`
}
let warn
beforeEach(() => {
warn = jest.spyOn(log, 'warn')
warn.mockImplementation(() => {})
})
afterEach(() => {
warn.mockRestore()
})
2020-08-27 18:42:21 +02:00
function formatPrettier(input) {
2020-08-27 18:42:21 +02:00
return prettier.format(input, {
parser: 'css',
printWidth: 100,
})
}
function format(input) {
Bump `lightningcss` dependency (#11388) * bump `lightningcss` * allow for non-standard selector combinators such as `::deep` * just use Prettier when comparing CSS results We were using 2 different engines where the stable one was not using Lightning CSS and the Oxide one was using Lightning CSS. To ensure that we didn't have to rewrite every single test expectation, the `toMatchFormattedCss` parsed both the actual and expected value using Lightning CSS (to make the result similar), then it used Prettier to make it... pretty. Right now we _only_ use Lightning CSS, which means that we can drop the additional lightningcss format step and just use Prettier on both the actual and expected values. Pretty also only prettifies the CSS, it doesn't rewrite it. E.g.: `@media (min-width: 768px)` will not be optimized to `@media (width >= 768px)`, that's something that Lightning CSS does for us. This will require some changes in our test output, but it will be consistent afterwards because there won't be hidden transformation steps anymore. Because up until now it could be that the actual result was `color: black` but the tests showed `color: #000` (because it is shorter). This change will reflect reality. * update tests based on previous commit * only use `toMatchFormattedCss` instead of `toMatchCss` They both do the exact same thing right now. While `toMatchCss` is shorter, `toMatchFormattedCss` makes a bit more sense since we are comparing the Prettier results. * update integration tests
2023-06-07 15:56:32 +02:00
return formatPrettier(input).replace(/\n{2,}/g, '\n')
}
2020-08-27 18:42:21 +02:00
function toMatchFormattedCss(received = '', argument = '') {
let options = {
comment: 'formatCSS(received) === formatCSS(argument)',
isNot: this.isNot,
promise: this.promise,
}
// Drop the license from the tests such that we can purely focus on the actual CSS being
// generated.
received = received.replace(license(), '')
argument = argument.replace(license(), '')
let formattedReceived = format(received)
let formattedArgument = format(argument)
let pass = formattedReceived === formattedArgument
let message = pass
? () => {
return (
this.utils.matcherHint('toMatchFormattedCss', undefined, undefined, options) +
'\n\n' +
`Expected: not ${this.utils.printExpected(formattedReceived)}\n` +
`Received: ${this.utils.printReceived(formattedArgument)}`
)
}
: () => {
let diffString = diff(formattedArgument, formattedReceived, {
expand: this.expand,
})
return (
this.utils.matcherHint('toMatchFormattedCss', undefined, undefined, options) +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
Bump `lightningcss` dependency (#11388) * bump `lightningcss` * allow for non-standard selector combinators such as `::deep` * just use Prettier when comparing CSS results We were using 2 different engines where the stable one was not using Lightning CSS and the Oxide one was using Lightning CSS. To ensure that we didn't have to rewrite every single test expectation, the `toMatchFormattedCss` parsed both the actual and expected value using Lightning CSS (to make the result similar), then it used Prettier to make it... pretty. Right now we _only_ use Lightning CSS, which means that we can drop the additional lightningcss format step and just use Prettier on both the actual and expected values. Pretty also only prettifies the CSS, it doesn't rewrite it. E.g.: `@media (min-width: 768px)` will not be optimized to `@media (width >= 768px)`, that's something that Lightning CSS does for us. This will require some changes in our test output, but it will be consistent afterwards because there won't be hidden transformation steps anymore. Because up until now it could be that the actual result was `color: black` but the tests showed `color: #000` (because it is shorter). This change will reflect reality. * update tests based on previous commit * only use `toMatchFormattedCss` instead of `toMatchCss` They both do the exact same thing right now. While `toMatchCss` is shorter, `toMatchFormattedCss` makes a bit more sense since we are comparing the Prettier results. * update integration tests
2023-06-07 15:56:32 +02:00
: `Expected: ${this.utils.printExpected(formattedArgument)}\n` +
`Received: ${this.utils.printReceived(formattedReceived)}`)
)
}
return { actual: received, message, pass }
}
2020-08-27 18:42:21 +02:00
expect.extend({
toMatchFormattedCss: toMatchFormattedCss,
toIncludeCss(received, argument) {
let options = {
Bump `lightningcss` dependency (#11388) * bump `lightningcss` * allow for non-standard selector combinators such as `::deep` * just use Prettier when comparing CSS results We were using 2 different engines where the stable one was not using Lightning CSS and the Oxide one was using Lightning CSS. To ensure that we didn't have to rewrite every single test expectation, the `toMatchFormattedCss` parsed both the actual and expected value using Lightning CSS (to make the result similar), then it used Prettier to make it... pretty. Right now we _only_ use Lightning CSS, which means that we can drop the additional lightningcss format step and just use Prettier on both the actual and expected values. Pretty also only prettifies the CSS, it doesn't rewrite it. E.g.: `@media (min-width: 768px)` will not be optimized to `@media (width >= 768px)`, that's something that Lightning CSS does for us. This will require some changes in our test output, but it will be consistent afterwards because there won't be hidden transformation steps anymore. Because up until now it could be that the actual result was `color: black` but the tests showed `color: #000` (because it is shorter). This change will reflect reality. * update tests based on previous commit * only use `toMatchFormattedCss` instead of `toMatchCss` They both do the exact same thing right now. While `toMatchCss` is shorter, `toMatchFormattedCss` makes a bit more sense since we are comparing the Prettier results. * update integration tests
2023-06-07 15:56:32 +02:00
comment: 'formatCSS(received).includes(formatCSS(argument))',
isNot: this.isNot,
promise: this.promise,
}
let pass = format(received).includes(format(argument))
let message = pass
? () => {
return (
this.utils.matcherHint('toIncludeCss', undefined, undefined, options) +
'\n\n' +
`Expected: not ${this.utils.printExpected(formatPrettier(received))}\n` +
`Received: ${this.utils.printReceived(formatPrettier(argument))}`
)
}
: () => {
let actual = formatPrettier(received)
let expected = formatPrettier(argument)
let diffString = diff(expected, actual, {
expand: this.expand,
})
return (
this.utils.matcherHint('toIncludeCss', undefined, undefined, options) +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expected)}\n` +
`Received: ${this.utils.printReceived(actual)}`)
)
}
return { actual: received, message, pass }
},
toHaveBeenWarned() {
let passed = warn.mock.calls.length > 0
if (passed) {
return {
pass: true,
message: () => {
return (
this.utils.matcherHint('toHaveBeenWarned') +
'\n\n' +
`Expected number of calls: >= ${this.utils.printExpected(1)}\n` +
`Received number of calls: ${this.utils.printReceived(actualWarningKeys.length)}`
)
},
}
} else {
return {
pass: false,
message: () => {
return (
this.utils.matcherHint('toHaveBeenWarned') +
'\n\n' +
`Expected number of calls: >= ${this.utils.printExpected(1)}\n` +
`Received number of calls: ${this.utils.printReceived(warn.mock.calls.length)}`
)
},
}
}
},
toHaveBeenWarnedWith(_received, expectedWarningKeys) {
let actualWarningKeys = warn.mock.calls.map((args) => args[0])
let passed = expectedWarningKeys.every((key) => actualWarningKeys.includes(key))
if (passed) {
return {
pass: true,
message: () => {
return (
this.utils.matcherHint('toHaveBeenWarnedWith') +
'\n\n' +
`Expected: not ${this.utils.printExpected(expectedWarningKeys)}\n` +
`Received: ${this.utils.printReceived(actualWarningKeys)}`
)
},
}
} else {
let diffString = diff(expectedWarningKeys, actualWarningKeys)
return {
pass: false,
message: () => {
return (
this.utils.matcherHint('toHaveBeenWarnedWith') +
'\n\n' +
(diffString && diffString.includes('- Expect')
? `Difference:\n\n${diffString}`
: `Expected: ${this.utils.printExpected(expectedWarningKeys)}\n` +
`Received: ${this.utils.printReceived(actualWarningKeys)}`)
)
},
}
}
},
})