]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for options. | |
3 | * @author Sindre Sorhus | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const assert = require("chai").assert, | |
13 | chalk = require("chalk"), | |
14 | proxyquire = require("proxyquire"), | |
15 | sinon = require("sinon"); | |
16 | ||
609c276f TL |
17 | //----------------------------------------------------------------------------- |
18 | // Helpers | |
19 | //----------------------------------------------------------------------------- | |
20 | ||
eb39fafa DC |
21 | /* |
22 | * Chalk protects its methods so we need to inherit from it | |
23 | * for Sinon to work. | |
24 | */ | |
25 | const chalkStub = Object.create(chalk, { | |
26 | reset: { | |
27 | value(str) { | |
28 | return chalk.reset(str); | |
29 | }, | |
30 | writable: true | |
31 | }, | |
32 | yellow: { | |
33 | value(str) { | |
34 | return chalk.yellow(str); | |
35 | }, | |
36 | writable: true | |
37 | }, | |
38 | red: { | |
39 | value(str) { | |
40 | return chalk.red(str); | |
41 | }, | |
42 | writable: true | |
43 | } | |
44 | }); | |
45 | ||
46 | chalkStub.yellow.bold = chalk.yellow.bold; | |
47 | chalkStub.red.bold = chalk.red.bold; | |
48 | ||
49 | const formatter = proxyquire("../../../../lib/cli-engine/formatters/stylish", { chalk: chalkStub }); | |
50 | ||
51 | //------------------------------------------------------------------------------ | |
52 | // Tests | |
53 | //------------------------------------------------------------------------------ | |
54 | ||
55 | describe("formatter:stylish", () => { | |
56 | const originalColorLevel = chalk.level; | |
57 | ||
58 | beforeEach(() => { | |
59 | chalk.level = 0; | |
60 | sinon.spy(chalkStub, "reset"); | |
61 | sinon.spy(chalkStub.yellow, "bold"); | |
62 | sinon.spy(chalkStub.red, "bold"); | |
63 | }); | |
64 | ||
65 | afterEach(() => { | |
66 | sinon.verifyAndRestore(); | |
67 | chalk.level = originalColorLevel; | |
68 | }); | |
69 | ||
70 | describe("when passed no messages", () => { | |
71 | const code = [{ | |
72 | filePath: "foo.js", | |
73 | messages: [], | |
74 | errorCount: 0, | |
75 | warningCount: 0 | |
76 | }]; | |
77 | ||
78 | it("should not return message", () => { | |
79 | const result = formatter(code); | |
80 | ||
81 | assert.strictEqual(result, ""); | |
82 | assert.strictEqual(chalkStub.reset.callCount, 0); | |
83 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
84 | assert.strictEqual(chalkStub.red.bold.callCount, 0); | |
85 | }); | |
86 | }); | |
87 | ||
88 | describe("when passed a single error message", () => { | |
89 | const code = [{ | |
90 | filePath: "foo.js", | |
91 | errorCount: 1, | |
92 | warningCount: 0, | |
93 | fixableErrorCount: 0, | |
94 | fixableWarningCount: 0, | |
95 | messages: [{ | |
96 | message: "Unexpected foo.", | |
97 | severity: 2, | |
98 | line: 5, | |
99 | column: 10, | |
100 | ruleId: "foo" | |
101 | }] | |
102 | }]; | |
103 | ||
104 | it("should return a string in the correct format", () => { | |
105 | const result = formatter(code); | |
106 | ||
107 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n\n\u2716 1 problem (1 error, 0 warnings)\n"); | |
108 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
109 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
110 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
111 | ||
112 | }); | |
113 | ||
114 | describe("when the error is fixable", () => { | |
115 | beforeEach(() => { | |
116 | code[0].fixableErrorCount = 1; | |
117 | }); | |
118 | ||
119 | it("should return a string in the correct format", () => { | |
120 | const result = formatter(code); | |
121 | ||
122 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n\n\u2716 1 problem (1 error, 0 warnings)\n 1 error and 0 warnings potentially fixable with the `--fix` option.\n"); | |
123 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
124 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
125 | assert.strictEqual(chalkStub.red.bold.callCount, 2); | |
126 | }); | |
127 | }); | |
128 | }); | |
129 | ||
130 | describe("when passed a single warning message", () => { | |
131 | const code = [{ | |
132 | filePath: "foo.js", | |
133 | errorCount: 0, | |
134 | warningCount: 1, | |
135 | fixableErrorCount: 0, | |
136 | fixableWarningCount: 0, | |
137 | messages: [{ | |
138 | message: "Unexpected foo.", | |
139 | severity: 1, | |
140 | line: 5, | |
141 | column: 10, | |
142 | ruleId: "foo" | |
143 | }] | |
144 | }]; | |
145 | ||
146 | it("should return a string in the correct format", () => { | |
147 | const result = formatter(code); | |
148 | ||
149 | assert.strictEqual(result, "\nfoo.js\n 5:10 warning Unexpected foo foo\n\n\u2716 1 problem (0 errors, 1 warning)\n"); | |
150 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
151 | assert.strictEqual(chalkStub.yellow.bold.callCount, 1); | |
152 | assert.strictEqual(chalkStub.red.bold.callCount, 0); | |
153 | }); | |
154 | ||
155 | describe("when the error is fixable", () => { | |
156 | beforeEach(() => { | |
157 | code[0].fixableWarningCount = 1; | |
158 | }); | |
159 | ||
160 | it("should return a string in the correct format", () => { | |
161 | const result = formatter(code); | |
162 | ||
163 | assert.strictEqual(result, "\nfoo.js\n 5:10 warning Unexpected foo foo\n\n\u2716 1 problem (0 errors, 1 warning)\n 0 errors and 1 warning potentially fixable with the `--fix` option.\n"); | |
164 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
165 | assert.strictEqual(chalkStub.yellow.bold.callCount, 2); | |
166 | assert.strictEqual(chalkStub.red.bold.callCount, 0); | |
167 | }); | |
168 | ||
169 | }); | |
170 | }); | |
171 | ||
172 | describe("when passed a message that ends with ' .'", () => { | |
173 | const code = [{ | |
174 | filePath: "foo.js", | |
175 | errorCount: 0, | |
176 | warningCount: 1, | |
177 | fixableErrorCount: 0, | |
178 | fixableWarningCount: 0, | |
179 | messages: [{ | |
180 | message: "Unexpected .", | |
181 | severity: 1, | |
182 | line: 5, | |
183 | column: 10, | |
184 | ruleId: "foo" | |
185 | }] | |
186 | }]; | |
187 | ||
188 | it("should return a string in the correct format (retaining the ' .')", () => { | |
189 | const result = formatter(code); | |
190 | ||
191 | assert.strictEqual(result, "\nfoo.js\n 5:10 warning Unexpected . foo\n\n\u2716 1 problem (0 errors, 1 warning)\n"); | |
192 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
193 | assert.strictEqual(chalkStub.yellow.bold.callCount, 1); | |
194 | assert.strictEqual(chalkStub.red.bold.callCount, 0); | |
195 | }); | |
196 | }); | |
197 | ||
198 | describe("when passed a fatal error message", () => { | |
199 | const code = [{ | |
200 | filePath: "foo.js", | |
201 | errorCount: 1, | |
202 | warningCount: 0, | |
203 | messages: [{ | |
204 | fatal: true, | |
205 | message: "Unexpected foo.", | |
206 | line: 5, | |
207 | column: 10, | |
208 | ruleId: "foo" | |
209 | }] | |
210 | }]; | |
211 | ||
212 | it("should return a string in the correct format", () => { | |
213 | const result = formatter(code); | |
214 | ||
215 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n\n\u2716 1 problem (1 error, 0 warnings)\n"); | |
216 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
217 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
218 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
219 | }); | |
220 | }); | |
221 | ||
222 | describe("when passed multiple messages", () => { | |
223 | const code = [{ | |
224 | filePath: "foo.js", | |
225 | errorCount: 1, | |
226 | warningCount: 1, | |
227 | messages: [{ | |
228 | message: "Unexpected foo.", | |
229 | severity: 2, | |
230 | line: 5, | |
231 | column: 10, | |
232 | ruleId: "foo" | |
233 | }, { | |
234 | message: "Unexpected bar.", | |
235 | severity: 1, | |
236 | line: 6, | |
237 | column: 11, | |
238 | ruleId: "bar" | |
239 | }] | |
240 | }]; | |
241 | ||
242 | it("should return a string with multiple entries", () => { | |
243 | const result = formatter(code); | |
244 | ||
245 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n 6:11 warning Unexpected bar bar\n\n\u2716 2 problems (1 error, 1 warning)\n"); | |
246 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
247 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
248 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
249 | }); | |
250 | }); | |
251 | ||
252 | describe("when passed multiple files with 1 message each", () => { | |
253 | const code = [{ | |
254 | filePath: "foo.js", | |
255 | errorCount: 1, | |
256 | warningCount: 0, | |
257 | messages: [{ | |
258 | message: "Unexpected foo.", | |
259 | severity: 2, | |
260 | line: 5, | |
261 | column: 10, | |
262 | ruleId: "foo" | |
263 | }] | |
264 | }, { | |
265 | errorCount: 0, | |
266 | warningCount: 1, | |
267 | filePath: "bar.js", | |
268 | messages: [{ | |
269 | message: "Unexpected bar.", | |
270 | severity: 1, | |
271 | line: 6, | |
272 | column: 11, | |
273 | ruleId: "bar" | |
274 | }] | |
275 | }]; | |
276 | ||
277 | it("should return a string with multiple entries", () => { | |
278 | const result = formatter(code); | |
279 | ||
280 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n\nbar.js\n 6:11 warning Unexpected bar bar\n\n\u2716 2 problems (1 error, 1 warning)\n"); | |
281 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
282 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
283 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
284 | }); | |
285 | ||
286 | it("should add errorCount", () => { | |
287 | code.forEach(c => { | |
288 | c.errorCount = 1; | |
289 | c.warningCount = 0; | |
290 | }); | |
291 | ||
292 | const result = formatter(code); | |
293 | ||
294 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n\nbar.js\n 6:11 warning Unexpected bar bar\n\n\u2716 2 problems (2 errors, 0 warnings)\n"); | |
295 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
296 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
297 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
298 | }); | |
299 | ||
300 | it("should add warningCount", () => { | |
301 | code.forEach(c => { | |
302 | c.errorCount = 0; | |
303 | c.warningCount = 1; | |
304 | }); | |
305 | ||
306 | const result = formatter(code); | |
307 | ||
308 | assert.strictEqual(result, "\nfoo.js\n 5:10 error Unexpected foo foo\n\nbar.js\n 6:11 warning Unexpected bar bar\n\n\u2716 2 problems (0 errors, 2 warnings)\n"); | |
309 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
310 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
311 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
312 | }); | |
313 | }); | |
314 | ||
315 | describe("when passed one file not found message", () => { | |
316 | const code = [{ | |
317 | filePath: "foo.js", | |
318 | errorCount: 1, | |
319 | warningCount: 0, | |
320 | messages: [{ | |
321 | fatal: true, | |
322 | message: "Couldn't find foo.js." | |
323 | }] | |
324 | }]; | |
325 | ||
326 | it("should return a string without line and column", () => { | |
327 | const result = formatter(code); | |
328 | ||
329 | assert.strictEqual(result, "\nfoo.js\n 0:0 error Couldn't find foo.js\n\n\u2716 1 problem (1 error, 0 warnings)\n"); | |
330 | assert.strictEqual(chalkStub.reset.callCount, 1); | |
331 | assert.strictEqual(chalkStub.yellow.bold.callCount, 0); | |
332 | assert.strictEqual(chalkStub.red.bold.callCount, 1); | |
333 | }); | |
334 | }); | |
335 | ||
336 | describe("fixable problems", () => { | |
337 | it("should not output fixable problems message when no errors or warnings are fixable", () => { | |
338 | const code = [{ | |
339 | filePath: "foo.js", | |
340 | errorCount: 1, | |
341 | warningCount: 0, | |
342 | fixableErrorCount: 0, | |
343 | fixableWarningCount: 0, | |
344 | messages: [{ | |
345 | message: "Unexpected foo.", | |
346 | severity: 2, | |
347 | line: 5, | |
348 | column: 10, | |
349 | ruleId: "foo" | |
350 | }] | |
351 | }]; | |
352 | ||
353 | const result = formatter(code); | |
354 | ||
355 | assert.notInclude(result, "potentially fixable"); | |
356 | }); | |
357 | ||
358 | it("should output the fixable problems message when errors are fixable", () => { | |
359 | const code = [{ | |
360 | filePath: "foo.js", | |
361 | errorCount: 1, | |
362 | warningCount: 0, | |
363 | fixableErrorCount: 1, | |
364 | fixableWarningCount: 0, | |
365 | messages: [{ | |
366 | message: "Unexpected foo.", | |
367 | severity: 2, | |
368 | line: 5, | |
369 | column: 10, | |
370 | ruleId: "foo" | |
371 | }] | |
372 | }]; | |
373 | ||
374 | const result = formatter(code); | |
375 | ||
376 | assert.include(result, " 1 error and 0 warnings potentially fixable with the `--fix` option.\n"); | |
377 | }); | |
378 | ||
379 | it("should output fixable problems message when warnings are fixable", () => { | |
380 | const code = [{ | |
381 | filePath: "foo.js", | |
382 | errorCount: 0, | |
383 | warningCount: 3, | |
384 | fixableErrorCount: 0, | |
385 | fixableWarningCount: 2, | |
386 | messages: [{ | |
387 | message: "Unexpected foo." | |
388 | }] | |
389 | }]; | |
390 | ||
391 | const result = formatter(code); | |
392 | ||
393 | assert.include(result, " 0 errors and 2 warnings potentially fixable with the `--fix` option.\n"); | |
394 | }); | |
395 | ||
396 | it("should output the total number of fixable errors and warnings", () => { | |
397 | const code = [{ | |
398 | filePath: "foo.js", | |
399 | errorCount: 5, | |
400 | warningCount: 3, | |
401 | fixableErrorCount: 5, | |
402 | fixableWarningCount: 2, | |
403 | messages: [{ | |
404 | message: "Unexpected foo." | |
405 | }] | |
406 | }, { | |
407 | filePath: "bar.js", | |
408 | errorCount: 4, | |
409 | warningCount: 2, | |
410 | fixableErrorCount: 4, | |
411 | fixableWarningCount: 1, | |
412 | messages: [{ | |
413 | message: "Unexpected bar." | |
414 | }] | |
415 | }]; | |
416 | ||
417 | const result = formatter(code); | |
418 | ||
419 | assert.include(result, " 9 errors and 3 warnings potentially fixable with the `--fix` option.\n"); | |
420 | }); | |
421 | }); | |
422 | }); |