|
1 |
| -'use strict'; // eslint-disable-line |
| 1 | +'use strict'; |
2 | 2 |
|
3 |
| -const _ = require('lodash'); |
4 | 3 | const stylelint = require('stylelint');
|
| 4 | +const getTestRule = require('jest-preset-stylelint/getTestRule'); |
5 | 5 |
|
6 |
| -global.testRule = (rule, schema) => { |
7 |
| - expect.extend({ |
8 |
| - toHaveMessage(testCase) { |
9 |
| - if (testCase.message === undefined) { |
10 |
| - return { |
11 |
| - message: () => 'Expected "reject" test case to have a "message" property', |
12 |
| - pass: false, |
13 |
| - }; |
14 |
| - } |
15 |
| - |
16 |
| - return { |
17 |
| - pass: true, |
18 |
| - }; |
19 |
| - }, |
20 |
| - }); |
21 |
| - |
22 |
| - describe(schema.ruleName, () => { |
23 |
| - const stylelintConfig = { |
24 |
| - plugins: ['./src'], |
25 |
| - rules: { |
26 |
| - [schema.ruleName]: schema.config, |
27 |
| - }, |
28 |
| - }; |
29 |
| - |
30 |
| - if (schema.accept && schema.accept.length) { |
31 |
| - describe('accept', () => { |
32 |
| - schema.accept.forEach(testCase => { |
33 |
| - const spec = testCase.only ? it.only : it; |
34 |
| - |
35 |
| - spec(testCase.description || 'no description', () => { |
36 |
| - const options = { |
37 |
| - code: testCase.code, |
38 |
| - config: stylelintConfig, |
39 |
| - syntax: schema.syntax, |
40 |
| - }; |
41 |
| - |
42 |
| - return stylelint.lint(options).then(output => { |
43 |
| - expect(output.results[0].warnings).toEqual([]); |
44 |
| - if (!schema.fix) { |
45 |
| - return; |
46 |
| - } |
47 |
| - |
48 |
| - // Check the fix |
49 |
| - return stylelint.lint(Object.assign({ fix: true }, options)).then(output2 => { |
50 |
| - const fixedCode = getOutputCss(output2); |
51 |
| - |
52 |
| - expect(fixedCode).toBe(testCase.code); |
53 |
| - }); |
54 |
| - }); |
55 |
| - }); |
56 |
| - }); |
57 |
| - }); |
58 |
| - } |
59 |
| - |
60 |
| - if (schema.reject && schema.reject.length) { |
61 |
| - describe('reject', () => { |
62 |
| - schema.reject.forEach(testCase => { |
63 |
| - const spec = testCase.only ? it.only : it; |
64 |
| - |
65 |
| - spec(testCase.description || 'no description', () => { |
66 |
| - const options = { |
67 |
| - code: testCase.code, |
68 |
| - config: stylelintConfig, |
69 |
| - syntax: schema.syntax, |
70 |
| - }; |
71 |
| - |
72 |
| - return stylelint.lint(options).then(output => { |
73 |
| - const warnings = output.results[0].warnings; |
74 |
| - const warning = warnings[0]; |
75 |
| - |
76 |
| - expect(warnings.length).toBeGreaterThanOrEqual(1); |
77 |
| - // expect(testCase).toHaveMessage(); |
78 |
| - |
79 |
| - if (testCase.message !== undefined) { |
80 |
| - expect(_.get(warning, 'text')).toBe(testCase.message); |
81 |
| - } |
82 |
| - |
83 |
| - if (testCase.line !== undefined) { |
84 |
| - expect(_.get(warning, 'line')).toBe(testCase.line); |
85 |
| - } |
86 |
| - |
87 |
| - if (testCase.column !== undefined) { |
88 |
| - expect(_.get(warning, 'column')).toBe(testCase.column); |
89 |
| - } |
90 |
| - |
91 |
| - if (!schema.fix) { |
92 |
| - return; |
93 |
| - } |
94 |
| - |
95 |
| - if (!testCase.fixed) { |
96 |
| - throw new Error( |
97 |
| - 'If using { fix: true } in test schema, all reject cases must have { fixed: .. }' |
98 |
| - ); |
99 |
| - } |
100 |
| - |
101 |
| - // Check the fix |
102 |
| - return stylelint.lint(Object.assign({ fix: true }, options)).then(output2 => { |
103 |
| - const fixedCode = getOutputCss(output2); |
104 |
| - |
105 |
| - expect(fixedCode).toBe(testCase.fixed); |
106 |
| - expect(output2.results[0].warnings.length).toBe(0); // Ensure errors are not reported on fixed code |
107 |
| - }); |
108 |
| - }); |
109 |
| - }); |
110 |
| - }); |
111 |
| - }); |
112 |
| - } |
113 |
| - }); |
114 |
| -}; |
115 |
| - |
116 |
| -function getOutputCss(output) { |
117 |
| - const result = output.results[0]._postcssResult; |
118 |
| - const css = result.root.toString(result.opts.syntax); |
119 |
| - |
120 |
| - return css; |
121 |
| -} |
122 |
| - |
123 |
| -global.testConfig = input => { |
124 |
| - let testFn; |
125 |
| - |
126 |
| - if (input.only) { |
127 |
| - testFn = test.only; |
128 |
| - } else if (input.skip) { |
129 |
| - testFn = test.skip; |
130 |
| - } else { |
131 |
| - testFn = test; |
132 |
| - } |
133 |
| - |
134 |
| - testFn(input.description, () => { |
135 |
| - const config = { |
136 |
| - plugins: ['./'], |
137 |
| - rules: { |
138 |
| - [input.ruleName]: input.config, |
139 |
| - }, |
140 |
| - }; |
141 |
| - |
142 |
| - return stylelint |
143 |
| - .lint({ |
144 |
| - code: '', |
145 |
| - config, |
146 |
| - }) |
147 |
| - .then(function(data) { |
148 |
| - const invalidOptionWarnings = data.results[0].invalidOptionWarnings; |
149 |
| - |
150 |
| - if (input.valid) { |
151 |
| - expect(invalidOptionWarnings.length).toBe(0); |
152 |
| - } else { |
153 |
| - expect(invalidOptionWarnings[0].text).toBe(input.message); |
154 |
| - } |
155 |
| - }); |
156 |
| - }); |
157 |
| -}; |
| 6 | +global.testRule = getTestRule(stylelint, { plugins: ['./src'] }); |
0 commit comments