-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest-setup.js
104 lines (87 loc) · 3.08 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
const { getObjectPath } = require('@funboxteam/diamonds');
const stylelint = require('stylelint');
global.testRule = (rule, schema) => {
describe(schema.ruleName, () => {
const stylelintConfig = {
plugins: ['./src'],
rules: {
[schema.ruleName]: schema.config,
},
};
if (schema.accept && schema.accept.length) {
describe('accept', () => {
schema.accept.forEach(testCase => {
const spec = testCase.only ? it.only : it;
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([]);
if (!schema.fix) {
return undefined;
}
// Check the fix
return stylelint
.lint({ fix: true, ...options })
.then(output2 => {
const fixedCode = getOutputCss(output2);
expect(fixedCode).toBe(testCase.code);
});
});
});
});
});
}
if (schema.reject && schema.reject.length) {
describe('reject', () => {
schema.reject.forEach(testCase => {
const spec = testCase.only ? it.only : it;
spec(testCase.description || 'no description', () => {
const options = {
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
};
return stylelint.lint(options).then(output => {
const warnings = output.results[0].warnings;
const warning = warnings[0];
expect(warnings.length).toBeGreaterThanOrEqual(1);
if (testCase.message !== undefined) {
expect(getObjectPath(warning, 'text')).toBe(testCase.message);
}
if (testCase.line !== undefined) {
expect(getObjectPath(warning, 'line')).toBe(testCase.line);
}
if (testCase.column !== undefined) {
expect(getObjectPath(warning, 'column')).toBe(testCase.column);
}
if (!schema.fix) {
return undefined;
}
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({ fix: true, ...options })
.then(output2 => {
const fixedCode = getOutputCss(output2);
expect(fixedCode).toBe(testCase.fixed);
});
});
});
});
});
}
});
};
function getOutputCss(output) {
// eslint-disable-next-line no-underscore-dangle
const result = output.results[0]._postcssResult;
return result.root.toString(result.opts.syntax);
}