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+ const options = {
58+ comment : 'stripped(received).includes(stripped(argument))' ,
59+ isNot : this . isNot ,
60+ promise : this . promise ,
61+ }
62+
63+ const actual = format ( received )
64+ const expected = format ( argument )
65+
66+ const pass = actual . includes ( expected )
67+
68+ const message = pass
69+ ? ( ) => {
70+ return (
71+ this . utils . matcherHint ( 'toIncludeCss' , undefined , undefined , options ) +
72+ '\n\n' +
73+ `Expected: not ${ this . utils . printExpected ( format ( received ) ) } \n` +
74+ `Received: ${ this . utils . printReceived ( format ( argument ) ) } `
75+ )
76+ }
77+ : ( ) => {
78+ const diffString = diff ( expected , actual , {
79+ expand : this . expand ,
80+ } )
81+
82+ return (
83+ this . utils . matcherHint ( 'toIncludeCss' , undefined , undefined , options ) +
84+ '\n\n' +
85+ ( diffString && diffString . includes ( '- Expect' )
86+ ? `Difference:\n\n${ diffString } `
87+ : `Expected: ${ this . utils . printExpected ( expected ) } \n` +
88+ `Received: ${ this . utils . printReceived ( actual ) } ` )
89+ )
90+ }
91+
92+ return { actual : received , message, pass }
93+ } ,
94+ } )
95+
96+ expect . extend ( {
97+ // Compare two CSS strings with all whitespace removed
98+ // This is probably naive but it's fast and works well enough.
99+ toMatchFormattedCss ( received , argument ) {
100+ function format ( input ) {
101+ return prettier . format ( input . replace ( / \n / g, '' ) , {
102+ parser : 'css' ,
103+ printWidth : 100 ,
104+ } )
105+ }
106+ const options = {
107+ comment : 'stripped(received) === stripped(argument)' ,
108+ isNot : this . isNot ,
109+ promise : this . promise ,
110+ }
111+
112+ let formattedReceived = format ( received )
113+ let formattedArgument = format ( argument )
114+
115+ const pass = formattedReceived === formattedArgument
116+
117+ const message = pass
118+ ? ( ) => {
119+ return (
120+ this . utils . matcherHint ( 'toMatchCss' , undefined , undefined , options ) +
121+ '\n\n' +
122+ `Expected: not ${ this . utils . printExpected ( formattedReceived ) } \n` +
123+ `Received: ${ this . utils . printReceived ( formattedArgument ) } `
124+ )
125+ }
126+ : ( ) => {
127+ const actual = formattedReceived
128+ const expected = formattedArgument
129+
130+ const diffString = diff ( expected , actual , {
131+ expand : this . expand ,
132+ } )
133+
134+ return (
135+ this . utils . matcherHint ( 'toMatchCss' , undefined , undefined , options ) +
136+ '\n\n' +
137+ ( diffString && diffString . includes ( '- Expect' )
138+ ? `Difference:\n\n${ diffString } `
139+ : `Expected: ${ this . utils . printExpected ( expected ) } \n` +
140+ `Received: ${ this . utils . printReceived ( actual ) } ` )
141+ )
142+ }
143+
144+ return { actual : received , message, pass }
145+ } ,
146+ } )
0 commit comments