forked from SimenB/csslint-stylish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
173 lines (135 loc) · 4.75 KB
/
test.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
/* eslint-env jest */
import { CSSLint } from 'csslint';
import stripAnsi from 'strip-ansi';
import path from 'path';
import reporter from './stylish';
test('should report stuff', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n');
let report =
reporter.startFormat() +
reporter.formatResults(res, path.resolve('style.css')) +
reporter.endFormat();
report = stripAnsi(report);
const filename = report.split('\n')[1];
expect(filename).toEqual('style.css', 'filename is correct');
expect(report).toMatch(/line 2/);
expect(report).toMatch(/char 3/);
expect(report).toMatch(/Use of !important/);
expect(report).toMatch(/1 warning/);
});
test('should report with full path', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n');
let report =
reporter.startFormat() +
reporter.formatResults(res, path.resolve('style.css'), {
absoluteFilePathsForFormatters: true,
}) +
reporter.endFormat();
report = stripAnsi(report);
const filename = report.split('\n')[1];
expect(filename).toEqual(
path.join(__dirname, 'style.css'),
'filename is correct'
);
expect(report).toMatch(/char 3/);
expect(report).toMatch(/Use of !important/);
expect(report).toMatch(/1 warning/);
});
test('should be able to be registered as formatter', () => {
expect(CSSLint.hasFormat('stylish')).toEqual(false);
CSSLint.addFormatter(reporter);
expect(CSSLint.hasFormat('stylish')).toEqual(true);
});
test('should not report undefined output lines when no filename provided', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n');
let report =
reporter.startFormat() + reporter.formatResults(res) + reporter.endFormat();
report = stripAnsi(report);
expect(report).not.toMatch(/^undefined$/gm);
});
test('should report filename provided', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n');
const filename = path.resolve('filenamestyle.css');
let report =
reporter.startFormat() +
reporter.formatResults(res, filename, {
absoluteFilePathsForFormatters: true,
}) +
reporter.endFormat();
report = stripAnsi(report);
expect(report).not.toMatch(/^undefined$/gm);
expect(report.split('\n')[1]).toEqual(
filename,
'filename should be in output lines'
);
});
test('should report no violations if there are none', () => {
const res = CSSLint.verify('.class {\n color: red;\n}\n');
const filename = path.resolve('filenamestyle.css');
let report =
reporter.startFormat() +
reporter.formatResults(res, filename) +
reporter.endFormat();
report = stripAnsi(report);
expect(report.trim()).toEqual('No violations');
});
test('should report errors', () => {
const res = CSSLint.verify('.class {\n color: red !important\n}\n', {
important: 2,
});
const filename = path.resolve('filenamestyle.css');
let report =
reporter.startFormat() +
reporter.formatResults(res, filename) +
reporter.endFormat();
report = stripAnsi(report);
expect(report).toMatch(/line 2/);
expect(report).toMatch(/char 3/);
expect(report).toMatch(/Use of !important/);
expect(report).toMatch(/1 error/);
});
test('should report multiple warnings', () => {
const res = CSSLint.verify(
'.class {\n color: red !important;\ntext-size: 12px !important;\n}\n',
{ important: 1 }
);
const filename = path.resolve('filenamestyle.css');
let report =
reporter.startFormat() +
reporter.formatResults(res, filename) +
reporter.endFormat();
report = stripAnsi(report);
expect(report).toMatch(/2 warnings/);
});
test('should report multiple errors', () => {
const res = CSSLint.verify(
'.class {\n color: red !important;\ntext-size: 12px !important;\n}\n',
{ important: 2 }
);
const filename = path.resolve('filenamestyle.css');
let report =
reporter.startFormat() +
reporter.formatResults(res, filename) +
reporter.endFormat();
report = stripAnsi(report);
expect(report).toMatch(/2 errors/);
});
test('should report rollups correctly', () => {
const res = CSSLint.verify(
'.class {\n float: left;\n float: left;\n float: left;\n float: left;\n float: left;\n float: left;' +
'\n float: left;\n float: left;\n float: left;\n float: left;\n float: left;\n}\n',
{ floats: 2 }
);
const filename = path.resolve('filenamestyle.css');
let report =
reporter.startFormat() +
reporter.formatResults(res, filename) +
reporter.endFormat();
report = stripAnsi(report);
expect(report).not.toMatch(/line /);
expect(report).not.toMatch(/char /);
expect(report).toMatch(
/Too many floats \(11\), you're probably using them for layout. Consider using a grid system instead\./
);
expect(report).toMatch(/1 warning/);
});