-
Notifications
You must be signed in to change notification settings - Fork 12
/
jest-setup.js
186 lines (167 loc) · 4.59 KB
/
jest-setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
// JSDOM
const { JSDOM } = require( 'jsdom' );
const jsdom = new JSDOM(
'<!doctype html><html><body><div class="test"></div></body></html>'
);
const { window } = jsdom;
global.window = window;
global.document = window.document;
// Please note, this is copy pasta from: https://github.com/kristerkari/stylelint-high-performance-animation/blob/master/jest-setup.js
// Styleint does not provide a tester for Jest: https://github.com/stylelint/stylelint/issues/815
const _ = require( 'lodash' );
const stylelint = require( 'stylelint' );
global.testRule = ( rule, schema ) => {
expect.extend( {
toHaveMessage( testCase ) {
if ( testCase.message === undefined ) {
return {
message: () =>
'Expected "reject" test case to have a "message" property',
pass: false,
};
}
return {
pass: true,
};
},
} );
describe( schema.ruleName, () => {
const stylelintConfig = {
plugins: [ './packages/stylelint-css-algorithms/index' ],
rules: {
[ schema.ruleName ]: schema.config,
},
};
const passingTestCases = schema.accept || [];
if ( passingTestCases && passingTestCases.length ) {
describe( 'accept', () => {
passingTestCases.forEach( ( testCase ) => {
const spec = testCase.only ? it.only : it;
describe( JSON.stringify( schema.config ), () => {
describe( JSON.stringify( testCase.code ), () => {
spec(
testCase.description || 'no description',
() => {
const options = {
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
};
return stylelint
.lint( options )
.then( ( output ) => {
expect(
output.results[ 0 ].warnings
).toEqual( [] );
expect(
output.results[ 0 ].parseErrors
).toEqual( [] );
if ( ! schema.fix ) return;
// Check the fix
return stylelint
.lint(
Object.assign(
{ fix: true },
options
)
)
.then( ( output ) => {
const fixedCode = getOutputCss(
output
);
expect( fixedCode ).toBe(
testCase.code
);
} );
} );
}
);
} );
} );
} );
} );
}
if ( schema.reject && schema.reject.length ) {
describe( 'reject', () => {
schema.reject.forEach( ( testCase ) => {
const spec = testCase.only ? it.only : it;
describe( JSON.stringify( schema.config ), () => {
describe( JSON.stringify( testCase.code ), () => {
spec(
testCase.description || 'no description',
() => {
const options = {
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
};
return stylelint
.lint( options )
.then( ( output ) => {
const warning =
output.results[ 0 ]
.warnings[ 0 ];
expect(
output.results[ 0 ].parseErrors
).toEqual( [] );
expect( testCase ).toHaveMessage();
if (
testCase.message !== undefined
) {
expect(
_.get( warning, 'text' )
).toBe( testCase.message );
}
if ( testCase.line !== undefined ) {
expect(
_.get( warning, 'line' )
).toBe( testCase.line );
}
if (
testCase.column !== undefined
) {
expect(
_.get( warning, 'column' )
).toBe( testCase.column );
}
if ( ! schema.fix ) return;
if ( ! testCase.fixed ) {
throw new Error(
'If using { fix: true } in test schema, all reject cases must have { fixed: .. }'
);
}
// Check the fix
return stylelint
.lint(
Object.assign(
{ fix: true },
options
)
)
.then( ( output ) => {
const fixedCode = getOutputCss(
output
);
expect( fixedCode ).toBe(
testCase.fixed
);
expect(
fixedCode
).not.toBe( testCase.code );
} );
} );
}
);
} );
} );
} );
} );
}
} );
};
function getOutputCss( output ) {
const result = output.results[ 0 ]._postcssResult;
const css = result.root.toString( result.opts.syntax );
return css;
}