2021-04-02 13:51:48 -04:00
|
|
|
const prettier = require('prettier')
|
2021-06-07 19:50:53 +02:00
|
|
|
const { diff } = require('jest-diff')
|
2023-03-17 14:45:10 +01:00
|
|
|
const log = require('../src/util/log').default
|
2023-06-22 16:41:37 -04:00
|
|
|
const { version } = require('../package.json')
|
|
|
|
|
|
|
|
|
|
function license() {
|
|
|
|
|
return `/* ! tailwindcss v${version} | MIT License | https://tailwindcss.com */\n`
|
|
|
|
|
}
|
2023-03-17 14:45:10 +01:00
|
|
|
|
|
|
|
|
let warn
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
warn = jest.spyOn(log, 'warn')
|
|
|
|
|
warn.mockImplementation(() => {})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
warn.mockRestore()
|
|
|
|
|
})
|
2020-08-27 18:42:21 +02:00
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
function formatPrettier(input) {
|
2020-08-27 18:42:21 +02:00
|
|
|
return prettier.format(input, {
|
|
|
|
|
parser: 'css',
|
|
|
|
|
printWidth: 100,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
function format(input) {
|
2023-06-07 15:56:32 +02:00
|
|
|
return formatPrettier(input).replace(/\n{2,}/g, '\n')
|
2023-01-23 20:44:31 +01:00
|
|
|
}
|
2020-08-27 18:42:21 +02:00
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
function toMatchFormattedCss(received = '', argument = '') {
|
|
|
|
|
let options = {
|
|
|
|
|
comment: 'formatCSS(received) === formatCSS(argument)',
|
|
|
|
|
isNot: this.isNot,
|
|
|
|
|
promise: this.promise,
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 16:41:37 -04:00
|
|
|
// 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(), '')
|
|
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
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)}`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
: () => {
|
2023-06-09 16:04:24 +02:00
|
|
|
let diffString = diff(formattedArgument, formattedReceived, {
|
2023-01-23 20:44:31 +01:00
|
|
|
expand: this.expand,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
this.utils.matcherHint('toMatchFormattedCss', undefined, undefined, options) +
|
|
|
|
|
'\n\n' +
|
|
|
|
|
(diffString && diffString.includes('- Expect')
|
|
|
|
|
? `Difference:\n\n${diffString}`
|
2023-06-07 15:56:32 +02:00
|
|
|
: `Expected: ${this.utils.printExpected(formattedArgument)}\n` +
|
|
|
|
|
`Received: ${this.utils.printReceived(formattedReceived)}`)
|
2023-01-23 20:44:31 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { actual: received, message, pass }
|
|
|
|
|
}
|
2020-08-27 18:42:21 +02:00
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
expect.extend({
|
|
|
|
|
toMatchFormattedCss: toMatchFormattedCss,
|
2021-05-18 17:21:35 +02:00
|
|
|
toIncludeCss(received, argument) {
|
2023-01-23 20:44:31 +01:00
|
|
|
let options = {
|
2023-06-07 15:56:32 +02:00
|
|
|
comment: 'formatCSS(received).includes(formatCSS(argument))',
|
2021-05-18 17:21:35 +02:00
|
|
|
isNot: this.isNot,
|
|
|
|
|
promise: this.promise,
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
let pass = format(received).includes(format(argument))
|
2021-05-18 17:21:35 +02:00
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
let message = pass
|
2021-05-18 17:21:35 +02:00
|
|
|
? () => {
|
|
|
|
|
return (
|
|
|
|
|
this.utils.matcherHint('toIncludeCss', undefined, undefined, options) +
|
|
|
|
|
'\n\n' +
|
2023-01-23 20:44:31 +01:00
|
|
|
`Expected: not ${this.utils.printExpected(formatPrettier(received))}\n` +
|
|
|
|
|
`Received: ${this.utils.printReceived(formatPrettier(argument))}`
|
2021-05-18 17:21:35 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
: () => {
|
2023-01-23 20:44:31 +01:00
|
|
|
let actual = formatPrettier(received)
|
|
|
|
|
let expected = formatPrettier(argument)
|
2021-05-18 17:21:35 +02:00
|
|
|
|
2023-01-23 20:44:31 +01:00
|
|
|
let diffString = diff(expected, actual, {
|
2021-05-18 17:21:35 +02:00
|
|
|
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 }
|
|
|
|
|
},
|
2023-03-17 14:45:10 +01:00
|
|
|
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)}`)
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2017-11-22 15:52:32 -05:00
|
|
|
})
|