Skip to content

Commit

Permalink
[Tests] use ESLint class in cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 25, 2021
1 parent 924b435 commit 4a9be63
Showing 1 changed file with 111 additions and 52 deletions.
163 changes: 111 additions & 52 deletions tests/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,144 @@
import path from 'path';

import { expect } from 'chai';
import { CLIEngine } from 'eslint';
import { CLIEngine, ESLint } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';
import * as importPlugin from '../../src/index';

describe('CLI regression tests', function () {
describe('issue #210', function () {
let eslint;
let cli;
before(function () {
cli = new CLIEngine({
useEslintrc: false,
configFile: './tests/files/issue210.config.js',
rulePaths: ['./src/rules'],
rules: {
'named': 2,
},
});
cli.addPlugin('eslint-plugin-import', importPlugin);
if (ESLint) {
eslint = new ESLint({
useEslintrc: false,
overrideConfigFile: './tests/files/issue210.config.js',
rulePaths: ['./src/rules'],
overrideConfig: {
rules: {
'named': 2,
},
},
plugins: { 'eslint-plugin-import': importPlugin },
});
} else {
cli = new CLIEngine({
useEslintrc: false,
configFile: './tests/files/issue210.config.js',
rulePaths: ['./src/rules'],
rules: {
'named': 2,
},
});
cli.addPlugin('eslint-plugin-import', importPlugin);
}
});
it("doesn't throw an error on gratuitous, erroneous self-reference", function () {
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw();
if (eslint) {
return eslint.lintFiles(['./tests/files/issue210.js'])
.catch(() => expect.fail());
} else {
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw();
}
});
});

describe('issue #1645', function () {
let eslint;
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,
});
cli.addPlugin('eslint-plugin-import', importPlugin);
if (ESLint) {
eslint = new ESLint({
useEslintrc: false,
overrideConfigFile: './tests/files/just-json-files/.eslintrc.json',
rulePaths: ['./src/rules'],
ignore: false,
plugins: { 'eslint-plugin-import': importPlugin },
});
} else {
cli = new CLIEngine({
useEslintrc: false,
configFile: './tests/files/just-json-files/.eslintrc.json',
rulePaths: ['./src/rules'],
ignore: false,
});
cli.addPlugin('eslint-plugin-import', importPlugin);
}
}
});

it('throws an error on invalid JSON', () => {
const invalidJSON = './tests/files/just-json-files/invalid.json';
const results = cli.executeOnFiles([invalidJSON]);
expect(results).to.eql({
results: [
{
filePath: path.resolve(invalidJSON),
messages: [
if (eslint) {
return eslint.lintFiles([invalidJSON]).then(results => {
expect(results).to.eql(
[
{
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: results.results[0].messages[0].source, // NewLine-characters might differ depending on git-settings
filePath: path.resolve(invalidJSON),
messages: [
{
column: 2,
endColumn: 3,
endLine: 1,
line: 1,
message: 'Expected a JSON object, array or literal.',
nodeType: results[0].messages[0].nodeType, // we don't care about this one
ruleId: 'json/*',
severity: 2,
source: results[0].messages[0].source, // NewLine-characters might differ depending on git-settings
},
],
errorCount: 1,
...(semver.satisfies(eslintPkg.version, '>= 7.32 || ^8.0.0-0') && {
fatalErrorCount: 0,
}),
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: results[0].source, // NewLine-characters might differ depending on git-settings
usedDeprecatedRules: results[0].usedDeprecatedRules, // we don't care about this one
},
],
errorCount: 1,
...(semver.satisfies(eslintPkg.version, '>= 7.32') && {
fatalErrorCount: 0,
}),
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: results.results[0].source, // NewLine-characters might differ depending on git-settings
},
],
...(semver.satisfies(eslintPkg.version, '>= 7.32') && {
fatalErrorCount: 0,
}),
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one
});
);
});
} else {
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: results.results[0].messages[0].source, // NewLine-characters might differ depending on git-settings
},
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: results.results[0].source, // NewLine-characters might differ depending on git-settings
},
],
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one
});
}
});
});
});

0 comments on commit 4a9be63

Please sign in to comment.