From 0547c7efa0a1becd2cdf35ae03f6fe74472398d9 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 --- .eslintignore | 1 + package.json | 1 + tests/files/just-json-files/.eslintrc.json | 22 +++++++++ tests/files/just-json-files/invalid.json | 1 + tests/src/cli.js | 55 ++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 tests/files/just-json-files/.eslintrc.json create mode 100644 tests/files/just-json-files/invalid.json diff --git a/.eslintignore b/.eslintignore index 93daf655e..b260021d4 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,6 +4,7 @@ coverage node_modules tests/files/malformed.js tests/files/with-syntax-error +tests/files/just-json-files/invalid.json resolvers/webpack/test/files # we want to ignore "tests/files" here, but unfortunately doing so would # interfere with unit test and fail it for some reason. diff --git a/package.json b/package.json index 0673205f3..6d3d512d2 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 000000000..4fbf13a72 --- /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 000000000..7edb2fa5b --- /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 93a4d43d7..53d6e8fdc 100644 --- a/tests/src/cli.js +++ b/tests/src/cli.js @@ -1,8 +1,12 @@ /** * tests that require fully booting up ESLint */ +import path from 'path' + import { expect } from 'chai' import { CLIEngine } from 'eslint' +import eslintPkg from 'eslint/package.json' +import semver from 'semver' describe('CLI regression tests', function () { describe('issue #210', function () { @@ -21,4 +25,55 @@ describe('CLI regression tests', function () { expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(Error) }) }) + + describe('issue #1645', function () { + let cli + beforeEach(function () { + if (semver.satisfies(eslintPkg.version, '< 6')) { + this.skip() + } else { + cli = new CLIEngine({ + useEslintrc: false, + configFile: './tests/files/just-json-files/.eslintrc.json', + rulePaths: ['./src/rules'], + ignore: false, + }) + } + }) + + 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 + }) + }) + }) })