]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/cli-engine/formatters/codeframe.js
2 * @fileoverview Tests for codeframe reporter.
3 * @author Vitor Balocco
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const assert
= require("chai").assert
;
13 const sinon
= require("sinon");
14 const proxyquire
= require("proxyquire");
15 const chalk
= require("chalk");
16 const path
= require("path");
17 const stripAnsi
= require("strip-ansi");
19 // Chalk protects its methods so we need to inherit from it for Sinon to work.
20 const chalkStub
= Object
.create(chalk
, {
23 return chalk
.yellow(str
);
29 return chalk
.red(str
);
35 chalkStub
.yellow
.bold
= chalk
.yellow
.bold
;
36 chalkStub
.red
.bold
= chalk
.red
.bold
;
38 const formatter
= proxyquire("../../../../lib/cli-engine/formatters/codeframe", { chalk
: chalkStub
});
40 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
44 describe("formatter:codeframe", () => {
46 sinon
.verifyAndRestore();
49 describe("when passed no messages", () => {
57 it("should return nothing", () => {
58 const result
= formatter(code
);
60 assert
.strictEqual(result
, "");
64 describe("when passed a single warning message", () => {
66 filePath
: path
.join(process
.cwd(), "lib", "foo.js"),
67 source
: "var foo = 1;\n var bar = 2;\n",
69 message
: "Unexpected foo.",
78 fixableWarningCount
: 0
81 it("should return a string in the correct format for warnings", () => {
82 const result
= formatter(code
);
84 assert
.strictEqual(stripAnsi(result
), [
85 `warning: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
95 it("should return bold yellow summary when there are only warnings", () => {
96 sinon
.spy(chalkStub
.yellow
, "bold");
97 sinon
.spy(chalkStub
.red
, "bold");
101 assert
.strictEqual(chalkStub
.yellow
.bold
.callCount
, 1);
102 assert
.strictEqual(chalkStub
.red
.bold
.callCount
, 0);
105 describe("when the warning is fixable", () => {
107 code
[0].fixableWarningCount
= 1;
110 it("should return a string in the correct format", () => {
111 const result
= formatter(code
);
113 assert
.strictEqual(stripAnsi(result
), [
114 `warning: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
115 "> 1 | var foo = 1;",
121 "1 warning potentially fixable with the `--fix` option."
127 describe("when passed a single error message", () => {
129 filePath
: path
.join(process
.cwd(), "lib", "foo.js"),
130 source
: "var foo = 1;\n var bar = 2;\n",
132 message
: "Unexpected foo.",
142 it("should return a string in the correct format for errors", () => {
143 const result
= formatter(code
);
145 assert
.strictEqual(stripAnsi(result
), [
146 `error: Unexpected foo (foo) at ${path.join("lib", "foo.js")}:1:5:`,
147 "> 1 | var foo = 1;",
156 it("should return bold red summary when there are errors", () => {
157 sinon
.spy(chalkStub
.yellow
, "bold");
158 sinon
.spy(chalkStub
.red
, "bold");
162 assert
.strictEqual(chalkStub
.yellow
.bold
.callCount
, 0);
163 assert
.strictEqual(chalkStub
.red
.bold
.callCount
, 1);
167 describe("when passed a message that ends with ' .'", () => {
172 message
: "Unexpected .",
180 it("should return a string in the correct format (retaining the ' .')", () => {
181 const result
= formatter(code
);
183 assert
.strictEqual(stripAnsi(result
), "error: Unexpected . (foo) at foo.js\n\n\n1 error found.");
187 describe("when passed multiple messages", () => {
190 source
: "const foo = 1\n",
192 message
: "Missing semicolon.",
198 message
: "'foo' is assigned a value but never used.",
202 ruleId
: "no-unused-vars"
208 it("should return a string with multiple entries", () => {
209 const result
= formatter(code
);
211 assert
.strictEqual(stripAnsi(result
), [
212 "error: Missing semicolon (semi) at foo.js:1:14:",
213 "> 1 | const foo = 1",
217 "error: 'foo' is assigned a value but never used (no-unused-vars) at foo.js:1:7:",
218 "> 1 | const foo = 1",
226 it("should return bold red summary when at least 1 of the messages is an error", () => {
227 sinon
.spy(chalkStub
.yellow
, "bold");
228 sinon
.spy(chalkStub
.red
, "bold");
229 code
[0].messages
[0].severity
= 1;
230 code
[0].warningCount
= 1;
231 code
[0].errorCount
= 1;
235 assert
.strictEqual(chalkStub
.yellow
.bold
.callCount
, 0);
236 assert
.strictEqual(chalkStub
.red
.bold
.callCount
, 1);
240 describe("when passed one file with 1 message and fixes applied", () => {
244 ruleId
: "no-unused-vars",
246 message
: "'foo' is assigned a value but never used.",
249 source
: " const foo = 1;"
253 output
: "function foo() {\n\n // a comment\n const foo = 1;\n}\n\n"
256 it("should return a string with code preview pointing to the correct location after fixes", () => {
257 const result
= formatter(code
);
259 assert
.strictEqual(stripAnsi(result
), [
260 "error: 'foo' is assigned a value but never used (no-unused-vars) at foo.js:4:11:",
263 "> 4 | const foo = 1;",
274 describe("when passed multiple files with 1 message each", () => {
277 source
: "const foo = 1\n",
279 message
: "Missing semicolon.",
289 source
: "const bar = 2\n",
291 message
: "Missing semicolon.",
301 it("should return a string with multiple entries", () => {
302 const result
= formatter(code
);
304 assert
.strictEqual(stripAnsi(result
), [
305 "error: Missing semicolon (semi) at foo.js:1:14:",
306 "> 1 | const foo = 1",
310 "error: Missing semicolon (semi) at bar.js:1:14:",
311 "> 1 | const bar = 2",
320 describe("when passed a fatal error message", () => {
329 message
: "Parsing error: Unexpected token {",
337 it("should return a string in the correct format", () => {
338 const result
= formatter(code
);
340 assert
.strictEqual(stripAnsi(result
), [
341 "error: Parsing error: Unexpected token { at foo.js:1:2:",
351 describe("when passed one file not found message", () => {
356 message
: "Couldn't find foo.js."
362 it("should return a string without code preview (codeframe)", () => {
363 const result
= formatter(code
);
365 assert
.strictEqual(stripAnsi(result
), "error: Couldn't find foo.js at foo.js\n\n\n1 error found.");
369 describe("when passed a single message with no line or column", () => {
374 message
: "Unexpected foo.",
382 it("should return a string without code preview (codeframe)", () => {
383 const result
= formatter(code
);
385 assert
.strictEqual(stripAnsi(result
), "error: Unexpected foo (foo) at foo.js\n\n\n1 error found.");
388 it("should output filepath but without 'line:column' appended", () => {
389 const result
= formatter(code
);
391 assert
.strictEqual(stripAnsi(result
), "error: Unexpected foo (foo) at foo.js\n\n\n1 error found.");
396 describe("fixable problems", () => {
397 it("should not output fixable problems message when no errors or warnings are fixable", () => {
402 fixableErrorCount
: 0,
403 fixableWarningCount
: 0,
405 message
: "Unexpected foo.",
413 const result
= formatter(code
);
415 assert
.notInclude(result
, "potentially fixable");
418 it("should output the fixable problems message when errors are fixable", () => {
423 fixableErrorCount
: 1,
424 fixableWarningCount
: 0,
426 message
: "Unexpected foo.",
434 const result
= formatter(code
);
436 assert
.include(result
, "1 error potentially fixable with the `--fix` option.");
439 it("should output fixable problems message when warnings are fixable", () => {
444 fixableErrorCount
: 0,
445 fixableWarningCount
: 2,
447 message
: "Unexpected foo."
451 const result
= formatter(code
);
453 assert
.include(result
, "2 warnings potentially fixable with the `--fix` option.");
456 it("should output the total number of fixable errors and warnings", () => {
461 fixableErrorCount
: 5,
462 fixableWarningCount
: 2,
464 message
: "Unexpected foo."
470 fixableErrorCount
: 4,
471 fixableWarningCount
: 1,
473 message
: "Unexpected bar."
477 const result
= formatter(code
);
479 assert
.include(result
, "9 errors and 3 warnings potentially fixable with the `--fix` option.");