|
| 1 | +const prettier = require('prettier') |
| 2 | +const { diff } = require('jest-diff') |
| 3 | + |
| 4 | +function format(input) { |
| 5 | + return prettier.format(input, { |
| 6 | + parser: 'css', |
| 7 | + printWidth: 100, |
| 8 | + }) |
| 9 | +} |
| 10 | + |
| 11 | +expect.extend({ |
| 12 | + // Compare two CSS strings with all whitespace removed |
| 13 | + // This is probably naive but it's fast and works well enough. |
| 14 | + toMatchCss(received, argument) { |
| 15 | + function stripped(str) { |
| 16 | + return str.replace(/\s/g, '').replace(/;/g, '') |
| 17 | + } |
| 18 | + |
| 19 | + const options = { |
| 20 | + comment: 'stripped(received) === stripped(argument)', |
| 21 | + isNot: this.isNot, |
| 22 | + promise: this.promise, |
| 23 | + } |
| 24 | + |
| 25 | + const pass = stripped(received) === stripped(argument) |
| 26 | + |
| 27 | + const message = pass |
| 28 | + ? () => { |
| 29 | + return ( |
| 30 | + this.utils.matcherHint('toMatchCss', undefined, undefined, options) + |
| 31 | + '\n\n' + |
| 32 | + `Expected: not ${this.utils.printExpected(format(received))}\n` + |
| 33 | + `Received: ${this.utils.printReceived(format(argument))}` |
| 34 | + ) |
| 35 | + } |
| 36 | + : () => { |
| 37 | + const actual = format(received) |
| 38 | + const expected = format(argument) |
| 39 | + |
| 40 | + const diffString = diff(expected, actual, { |
| 41 | + expand: this.expand, |
| 42 | + }) |
| 43 | + |
| 44 | + return ( |
| 45 | + this.utils.matcherHint('toMatchCss', undefined, undefined, options) + |
| 46 | + '\n\n' + |
| 47 | + (diffString && diffString.includes('- Expect') |
| 48 | + ? `Difference:\n\n${diffString}` |
| 49 | + : `Expected: ${this.utils.printExpected(expected)}\n` + |
| 50 | + `Received: ${this.utils.printReceived(actual)}`) |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + return { actual: received, message, pass } |
| 55 | + }, |
| 56 | + toIncludeCss(received, argument) { |
| 57 | + function stripped(str) { |
| 58 | + return str.replace(/\s/g, '').replace(/;/g, '') |
| 59 | + } |
| 60 | + |
| 61 | + const options = { |
| 62 | + comment: 'stripped(received).includes(stripped(argument))', |
| 63 | + isNot: this.isNot, |
| 64 | + promise: this.promise, |
| 65 | + } |
| 66 | + |
| 67 | + const pass = stripped(received).includes(stripped(argument)) |
| 68 | + |
| 69 | + const message = pass |
| 70 | + ? () => { |
| 71 | + return ( |
| 72 | + this.utils.matcherHint('toIncludeCss', undefined, undefined, options) + |
| 73 | + '\n\n' + |
| 74 | + `Expected: not ${this.utils.printExpected(format(received))}\n` + |
| 75 | + `Received: ${this.utils.printReceived(format(argument))}` |
| 76 | + ) |
| 77 | + } |
| 78 | + : () => { |
| 79 | + const actual = format(received) |
| 80 | + const expected = format(argument) |
| 81 | + |
| 82 | + const diffString = diff(expected, actual, { |
| 83 | + expand: this.expand, |
| 84 | + }) |
| 85 | + |
| 86 | + return ( |
| 87 | + this.utils.matcherHint('toIncludeCss', undefined, undefined, options) + |
| 88 | + '\n\n' + |
| 89 | + (diffString && diffString.includes('- Expect') |
| 90 | + ? `Difference:\n\n${diffString}` |
| 91 | + : `Expected: ${this.utils.printExpected(expected)}\n` + |
| 92 | + `Received: ${this.utils.printReceived(actual)}`) |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + return { actual: received, message, pass } |
| 97 | + }, |
| 98 | +}) |
| 99 | + |
| 100 | +expect.extend({ |
| 101 | + // Compare two CSS strings with all whitespace removed |
| 102 | + // This is probably naive but it's fast and works well enough. |
| 103 | + toMatchFormattedCss(received, argument) { |
| 104 | + function format(input) { |
| 105 | + return prettier.format(input.replace(/\n/g, ''), { |
| 106 | + parser: 'css', |
| 107 | + printWidth: 100, |
| 108 | + }) |
| 109 | + } |
| 110 | + const options = { |
| 111 | + comment: 'stripped(received) === stripped(argument)', |
| 112 | + isNot: this.isNot, |
| 113 | + promise: this.promise, |
| 114 | + } |
| 115 | + |
| 116 | + let formattedReceived = format(received) |
| 117 | + let formattedArgument = format(argument) |
| 118 | + |
| 119 | + const pass = formattedReceived === formattedArgument |
| 120 | + |
| 121 | + const message = pass |
| 122 | + ? () => { |
| 123 | + return ( |
| 124 | + this.utils.matcherHint('toMatchCss', undefined, undefined, options) + |
| 125 | + '\n\n' + |
| 126 | + `Expected: not ${this.utils.printExpected(formattedReceived)}\n` + |
| 127 | + `Received: ${this.utils.printReceived(formattedArgument)}` |
| 128 | + ) |
| 129 | + } |
| 130 | + : () => { |
| 131 | + const actual = formattedReceived |
| 132 | + const expected = formattedArgument |
| 133 | + |
| 134 | + const diffString = diff(expected, actual, { |
| 135 | + expand: this.expand, |
| 136 | + }) |
| 137 | + |
| 138 | + return ( |
| 139 | + this.utils.matcherHint('toMatchCss', undefined, undefined, options) + |
| 140 | + '\n\n' + |
| 141 | + (diffString && diffString.includes('- Expect') |
| 142 | + ? `Difference:\n\n${diffString}` |
| 143 | + : `Expected: ${this.utils.printExpected(expected)}\n` + |
| 144 | + `Received: ${this.utils.printReceived(actual)}`) |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + return { actual: received, message, pass } |
| 149 | + }, |
| 150 | +}) |
0 commit comments