From ca89d5e937299c18a2cf0a786b8f3c60cb25cc81 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 5 Jun 2020 15:06:15 -0700 Subject: [PATCH] [Tests] add test case for #1645 --- package.json | 1 + tests/files/just-json-files/.eslintrc.json | 22 ++++++++++ tests/files/just-json-files/invalid.json | 1 + tests/src/cli.js | 50 ++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 tests/files/just-json-files/.eslintrc.json create mode 100644 tests/files/just-json-files/invalid.json diff --git a/package.json b/package.json index 0673205f3b..6d3d512d2e 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "eslint-module-utils": "file:./utils", "eslint-plugin-eslint-plugin": "^2.2.1", "eslint-plugin-import": "2.x", + "eslint-plugin-json": "^2.1.1", "fs-copy-file-sync": "^1.1.1", "glob": "^7.1.6", "in-publish": "^2.0.0", diff --git a/tests/files/just-json-files/.eslintrc.json b/tests/files/just-json-files/.eslintrc.json new file mode 100644 index 0000000000..4fbf13a727 --- /dev/null +++ b/tests/files/just-json-files/.eslintrc.json @@ -0,0 +1,22 @@ +{ + "root": true, + + "plugins": ["import", "json"], + + "rules": { + "import/no-unused-modules": [ + "error", + { + "missingExports": false, + "unusedExports": true + } + ] + }, + + "overrides": [ + { + "files": "*.json", + "extends": "plugin:json/recommended" + } + ] +} diff --git a/tests/files/just-json-files/invalid.json b/tests/files/just-json-files/invalid.json new file mode 100644 index 0000000000..7edb2fa5bc --- /dev/null +++ b/tests/files/just-json-files/invalid.json @@ -0,0 +1 @@ +, diff --git a/tests/src/cli.js b/tests/src/cli.js index 93a4d43d79..f2c96699d5 100644 --- a/tests/src/cli.js +++ b/tests/src/cli.js @@ -1,6 +1,9 @@ /** * tests that require fully booting up ESLint */ +import fs from 'fs' +import path from 'path' + import { expect } from 'chai' import { CLIEngine } from 'eslint' @@ -21,4 +24,51 @@ describe('CLI regression tests', function () { expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(Error) }) }) + + const eslintConfig = JSON.parse(fs.readFileSync('./tests/files/just-json-files/.eslintrc.json')) + describe('issue #1645', function () { + let cli + beforeEach(function () { + cli = new CLIEngine({ + useEslintrc: false, + configFile: './tests/files/just-json-files/.eslintrc.json', + rulePaths: ['./src/rules'], + }) + }) + + it('throws an error on invalid JSON', function () { + const invalidJSON = './tests/files/just-json-files/invalid.json'; + const results = cli.executeOnFiles([invalidJSON]); + expect(results).to.eql({ + results: [ + { + filePath: path.resolve(invalidJSON), + messages: [ + { + column: 2, + endColumn: 3, + endLine: 1, + line: 1, + message: 'Expected a JSON object, array or literal.', + nodeType: results.results[0].messages[0].nodeType, // we don't care about this one + ruleId: 'json/*', + severity: 2, + source: '\n' + } + ], + errorCount: 1, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0, + source: ',\n' + } + ], + errorCount: 1, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0, + usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one + }); + }) + }) })