]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for "table" reporter. | |
3 | * @author Gajus Kuizinas <gajus@gajus.com> | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const assert = require("chai").assert; | |
12 | const chalk = require("chalk"); | |
13 | const formatter = require("../../../../lib/cli-engine/formatters/table"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | describe("formatter:table", () => { | |
20 | const originalColorLevel = chalk.level; | |
21 | ||
22 | before(() => { | |
23 | chalk.level = 0; | |
24 | }); | |
25 | ||
26 | after(() => { | |
27 | chalk.level = originalColorLevel; | |
28 | }); | |
29 | ||
30 | describe("when passed no messages", () => { | |
31 | const code = [ | |
32 | { | |
33 | filePath: "foo.js", | |
34 | messages: [], | |
35 | errorCount: 0, | |
36 | warningCount: 0 | |
37 | } | |
38 | ]; | |
39 | ||
40 | it("should return a table of error and warning count with no messages", () => { | |
41 | const expectedOutput = [ | |
42 | "", | |
43 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
44 | "║ 0 Errors ║", | |
45 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
46 | "║ 0 Warnings ║", | |
47 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
48 | "" | |
49 | ].join("\n"); | |
50 | ||
51 | const result = formatter(code); | |
52 | ||
53 | assert.strictEqual(result, expectedOutput); | |
54 | }); | |
55 | }); | |
56 | ||
57 | describe("when passed a single message", () => { | |
58 | it("should return a string in the correct format for errors", () => { | |
59 | const code = [ | |
60 | { | |
61 | filePath: "foo.js", | |
62 | messages: [ | |
63 | { | |
64 | message: "Unexpected foo.", | |
65 | severity: 2, | |
66 | line: 5, | |
67 | column: 10, | |
68 | ruleId: "foo" | |
69 | } | |
70 | ], | |
71 | errorCount: 1, | |
72 | warningCount: 0 | |
73 | } | |
74 | ]; | |
75 | ||
76 | const expectedOutput = [ | |
77 | "", | |
78 | "foo.js", | |
79 | "", | |
80 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
81 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
82 | "║ 5 │ 10 │ error │ Unexpected foo. │ foo ║", | |
83 | "", | |
84 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
85 | "║ 1 Error ║", | |
86 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
87 | "║ 0 Warnings ║", | |
88 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
89 | "" | |
90 | ].join("\n"); | |
91 | ||
92 | const result = formatter(code); | |
93 | ||
94 | assert.strictEqual(result, expectedOutput); | |
95 | }); | |
96 | ||
97 | it("should return a string in the correct format for warnings", () => { | |
98 | const code = [ | |
99 | { | |
100 | filePath: "foo.js", | |
101 | messages: [ | |
102 | { | |
103 | message: "Unexpected foo.", | |
104 | severity: 1, | |
105 | line: 5, | |
106 | column: 10, | |
107 | ruleId: "foo" | |
108 | } | |
109 | ], | |
110 | errorCount: 0, | |
111 | warningCount: 1 | |
112 | } | |
113 | ]; | |
114 | ||
115 | const expectedOutput = [ | |
116 | "", | |
117 | "foo.js", | |
118 | "", | |
119 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
120 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
121 | "║ 5 │ 10 │ warning │ Unexpected foo. │ foo ║", | |
122 | "", | |
123 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
124 | "║ 0 Errors ║", | |
125 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
126 | "║ 1 Warning ║", | |
127 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
128 | "" | |
129 | ].join("\n"); | |
130 | ||
131 | const result = formatter(code); | |
132 | ||
133 | assert.strictEqual(result, expectedOutput); | |
134 | }); | |
135 | }); | |
136 | ||
137 | describe("when passed a fatal error message", () => { | |
138 | it("should return a string in the correct format", () => { | |
139 | const code = [ | |
140 | { | |
141 | filePath: "foo.js", | |
142 | messages: [ | |
143 | { | |
144 | fatal: true, | |
145 | message: "Unexpected foo.", | |
146 | line: 5, | |
147 | column: 10, | |
148 | ruleId: "foo" | |
149 | } | |
150 | ], | |
151 | errorCount: 1, | |
152 | warningCount: 0 | |
153 | } | |
154 | ]; | |
155 | ||
156 | const expectedOutput = [ | |
157 | "", | |
158 | "foo.js", | |
159 | "", | |
160 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
161 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
162 | "║ 5 │ 10 │ error │ Unexpected foo. │ foo ║", | |
163 | "", | |
164 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
165 | "║ 1 Error ║", | |
166 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
167 | "║ 0 Warnings ║", | |
168 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
169 | "" | |
170 | ].join("\n"); | |
171 | ||
172 | const result = formatter(code); | |
173 | ||
174 | assert.strictEqual(result, expectedOutput); | |
175 | }); | |
176 | }); | |
177 | ||
178 | describe("when passed multiple messages", () => { | |
179 | it("should return a string with multiple entries", () => { | |
180 | const code = [ | |
181 | { | |
182 | filePath: "foo.js", | |
183 | messages: [ | |
184 | { | |
185 | message: "Unexpected foo.", | |
186 | severity: 2, | |
187 | line: 5, | |
188 | column: 10, | |
189 | ruleId: "foo" | |
190 | }, | |
191 | { | |
192 | message: "Unexpected bar.", | |
193 | severity: 1, | |
194 | line: 6, | |
195 | column: 11, | |
196 | ruleId: "bar" | |
197 | } | |
198 | ], | |
199 | errorCount: 1, | |
200 | warningCount: 1 | |
201 | } | |
202 | ]; | |
203 | ||
204 | const expectedOutput = [ | |
205 | "", | |
206 | "foo.js", | |
207 | "", | |
208 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
209 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
210 | "║ 5 │ 10 │ error │ Unexpected foo. │ foo ║", | |
211 | "║ 6 │ 11 │ warning │ Unexpected bar. │ bar ║", | |
212 | "", | |
213 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
214 | "║ 1 Error ║", | |
215 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
216 | "║ 1 Warning ║", | |
217 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
218 | "" | |
219 | ].join("\n"); | |
220 | ||
221 | const result = formatter(code); | |
222 | ||
223 | assert.strictEqual(result, expectedOutput); | |
224 | }); | |
225 | }); | |
226 | ||
227 | describe("when passed multiple files with 1 message each", () => { | |
228 | it("should return a string with multiple entries", () => { | |
229 | const code = [ | |
230 | { | |
231 | filePath: "foo.js", | |
232 | messages: [ | |
233 | { | |
234 | message: "Unexpected foo.", | |
235 | severity: 2, | |
236 | line: 5, | |
237 | column: 10, | |
238 | ruleId: "foo" | |
239 | } | |
240 | ], | |
241 | errorCount: 1, | |
242 | warningCount: 0 | |
243 | }, { | |
244 | filePath: "bar.js", | |
245 | messages: [ | |
246 | { | |
247 | message: "Unexpected bar.", | |
248 | severity: 1, | |
249 | line: 6, | |
250 | column: 11, | |
251 | ruleId: "bar" | |
252 | } | |
253 | ], | |
254 | errorCount: 0, | |
255 | warningCount: 1 | |
256 | } | |
257 | ]; | |
258 | ||
259 | const expectedOutput = [ | |
260 | "", | |
261 | "foo.js", | |
262 | "", | |
263 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
264 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
265 | "║ 5 │ 10 │ error │ Unexpected foo. │ foo ║", | |
266 | "", | |
267 | "bar.js", | |
268 | "", | |
269 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
270 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
271 | "║ 6 │ 11 │ warning │ Unexpected bar. │ bar ║", | |
272 | "", | |
273 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
274 | "║ 1 Error ║", | |
275 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
276 | "║ 1 Warning ║", | |
277 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
278 | "" | |
279 | ].join("\n"); | |
280 | ||
281 | const result = formatter(code); | |
282 | ||
283 | assert.strictEqual(result, expectedOutput); | |
284 | }); | |
285 | }); | |
286 | ||
287 | describe("when passed one file not found message", () => { | |
288 | it("should return a string without line and column (0, 0)", () => { | |
289 | const code = [ | |
290 | { | |
291 | filePath: "foo.js", | |
292 | messages: [ | |
293 | { | |
294 | fatal: true, | |
295 | message: "Couldn't find foo.js." | |
296 | } | |
297 | ], | |
298 | errorCount: 1, | |
299 | warningCount: 0 | |
300 | } | |
301 | ]; | |
302 | ||
303 | const expectedOutput = [ | |
304 | "", | |
305 | "foo.js", | |
306 | "", | |
307 | "║ Line │ Column │ Type │ Message │ Rule ID ║", | |
308 | "╟──────────┼──────────┼──────────┼────────────────────────────────────────────────────────┼──────────────────────╢", | |
309 | "║ 0 │ 0 │ error │ Couldn't find foo.js. │ ║", | |
310 | "", | |
311 | "╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗", | |
312 | "║ 1 Error ║", | |
313 | "╟────────────────────────────────────────────────────────────────────────────────────────────────────────────────╢", | |
314 | "║ 0 Warnings ║", | |
315 | "╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝", | |
316 | "" | |
317 | ].join("\n"); | |
318 | ||
319 | const result = formatter(code); | |
320 | ||
321 | assert.strictEqual(result, expectedOutput); | |
322 | }); | |
323 | }); | |
324 | }); |