Skip to content

Commit

Permalink
Add more options (#45)
Browse files Browse the repository at this point in the history
* Add cache, ignorePattern and reportUnusedDisableDirectives

* Add fixDryRun

* Add maxWarnings

* Add quiet

* Default quiet to false

* Fix max warnings test determinism
  • Loading branch information
xdissent authored and rogeliog committed Sep 21, 2018
1 parent e6ad060 commit c811667
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,25 @@ jest-runner-eslint maps a lot of ESLint CLI arguments to config options. For exa

|option|default|example
|-----|-----|-----|
|cache|`false`|`"cache": true`
|cacheLocation|`.eslintcache`|`"cacheLocation": "/path/to/cache"`
|config|`null`|`"config": "/path/to/config"`
|env|`null`|`"env": "mocha"` or `"env": ["mocha", "other"]`
|ext|`[".js"]`|`"ext": ".jsx"` or `"ext": [".jsx", ".ts"]`
|fix|`false`|`"fix": true`
|fixDryRun|`false`|`"fixDryRun": true`
|format|`null`|`"format": "codeframe"`
|global|`[]`|`"global": "it"` or `"global": ["it", "describe"]`
|ignorePath|`null`|`"ignorePath": "/path/to/ignore"`
|ignorePattern|`[]`|`"ignorePattern": ["/path/to/ignore/*"]`
|maxWarnings|`-1`|`"maxWarnings": 0`
|noEslintrc|`false`|`"noEslintrc": true`
|noIgnore|`false`|`"noIgnore": true`
|noInlineConfig|`false`|`"noInlineConfig": true`
|parser|`espree`|`"parser": "flow"`
|parserOptions|`{}`|`"parserOptions": { "myOption": true }`
|plugin|`[]`|`"plugin": "prettier"` or `"plugin": ["pettier", "other"]`
|quiet|`true`|`"quiet": false`
|reportUnusedDisableDirectives|`false`|`"reportUnusedDisableDirectives": true`
|rules|`null`|`"rules": {"quotes": [2, "double"]}` or `"rules": {"quotes": [2, "double"], "no-console": 2}`
|rulesdir|`[]`|`"rulesdir": "/path/to/rules/dir"` or `"env": ["/path/to/rules/dir", "/path/to/other"]`
5 changes: 5 additions & 0 deletions integrationTests/__fixtures__/loud/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": "warn"
}
}
3 changes: 3 additions & 0 deletions integrationTests/__fixtures__/loud/__eslint__/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = 1;

console.log('a', a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
cliOptions: {
quiet: false,
},
};
4 changes: 4 additions & 0 deletions integrationTests/__fixtures__/loud/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
runner: '../../../',
testMatch: ['**/__eslint__/**/*.js'],
};
5 changes: 5 additions & 0 deletions integrationTests/__fixtures__/max-warnings/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": "warn"
}
}
5 changes: 5 additions & 0 deletions integrationTests/__fixtures__/max-warnings/__eslint__/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const a = 1;

console.log('a', a);
console.log('a', a);
console.log('a', a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
cliOptions: {
maxWarnings: 2,
},
};
4 changes: 4 additions & 0 deletions integrationTests/__fixtures__/max-warnings/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
runner: '../../../',
testMatch: ['**/__eslint__/**/*.js'],
};
17 changes: 17 additions & 0 deletions integrationTests/__snapshots__/loud.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Outputs warnings as console messages 1`] = `
"PASS integrationTests/__fixtures__/loud/__eslint__/file.js
● Console
console.warn
/mocked-path-to-jest-runner-mocha/integrationTests/__fixtures__/loud/__eslint__/file.js
3:1 warning Unexpected console statement no-console
✖ 1 problem (0 errors, 1 warning)
✓ ESLint
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time:
Ran all test suites.
"
`;
19 changes: 19 additions & 0 deletions integrationTests/__snapshots__/max-warnings.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Fails if more than max warnings 1`] = `
"FAIL integrationTests/__fixtures__/max-warnings/__eslint__/file.js
/mocked-path-to-jest-runner-mocha/integrationTests/__fixtures__/max-warnings/__eslint__/file.js
3:1 warning Unexpected console statement no-console
4:1 warning Unexpected console statement no-console
5:1 warning Unexpected console statement no-console
✖ 3 problems (0 errors, 3 warnings)
ESLint found too many warnings (maximum: 2).
✕ ESLint
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time:
Ran all test suites.
"
`;
5 changes: 5 additions & 0 deletions integrationTests/loud.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const runJest = require('./runJest');

it('Outputs warnings as console messages', () => {
return expect(runJest('loud')).resolves.toMatchSnapshot();
});
5 changes: 5 additions & 0 deletions integrationTests/max-warnings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const runJest = require('./runJest');

it('Fails if more than max warnings', () => {
return expect(runJest('max-warnings')).resolves.toMatchSnapshot();
});
48 changes: 43 additions & 5 deletions src/runESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,51 @@ const runESLint = ({ testPath, config }) => {

const { CLIEngine } = getLocalESLint(config);
const options = getESLintOptions(config);
const cli = new CLIEngine(options.cliOptions);
const quiet = options.cliOptions && options.cliOptions.quiet;
const cli = new CLIEngine(
Object.assign({}, options.cliOptions, {
fix:
options.cliOptions &&
(options.cliOptions.fix || options.cliOptions.fixDryRun) &&
(quiet ? ({ severity }) => severity === 2 : true),
}),
);
if (cli.isPathIgnored(testPath)) {
const end = Date.now();
return skip({ start, end, test: { path: testPath, title: 'ESLint' } });
}

const report = cli.executeOnFiles([testPath]);

if (options.cliOptions && options.cliOptions.fix) {
if (
options.cliOptions &&
options.cliOptions.fix &&
!options.cliOptions.fixDryRun
) {
CLIEngine.outputFixes(report);
}

const end = Date.now();

if (report.errorCount > 0) {
const tooManyWarnings =
options.cliOptions &&
options.cliOptions.maxWarnings != null &&
options.cliOptions.maxWarnings >= 0 &&
report.warningCount > options.cliOptions.maxWarnings;

const format = () => {
const formatter = cli.getFormatter(options.cliOptions.format);
const errorMessage = formatter(CLIEngine.getErrorResults(report.results));
return formatter(
quiet ? CLIEngine.getErrorResults(report.results) : report.results,
);
};

if (report.errorCount > 0 || tooManyWarnings) {
let errorMessage = format();

if (!report.errorCount && tooManyWarnings)
errorMessage += `\nESLint found too many warnings (maximum: ${options
.cliOptions.maxWarnings}).`;

return fail({
start,
Expand All @@ -37,7 +65,17 @@ const runESLint = ({ testPath, config }) => {
});
}

return pass({ start, end, test: { path: testPath, title: 'ESLint' } });
const result = pass({
start,
end,
test: { path: testPath, title: 'ESLint' },
});

if (!quiet && report.warningCount > 0) {
result.console = [{ message: format(), origin: '', type: 'warn' }];
}

return result;
};

module.exports = runESLint;
21 changes: 21 additions & 0 deletions src/utils/normalizeConfig.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const identity = v => v;
const negate = v => !v;
const asArray = v => (typeof v === 'string' ? [v] : v);
const asInt = v => (typeof v === 'number' ? v : parseInt(v, 10));

const BASE_CONFIG = {
cache: {
default: false,
},
cacheLocation: {
default: '.eslintcache',
},
Expand All @@ -23,6 +27,9 @@ const BASE_CONFIG = {
fix: {
default: false,
},
fixDryRun: {
default: false,
},
format: {
default: null,
},
Expand All @@ -34,6 +41,14 @@ const BASE_CONFIG = {
ignorePath: {
default: null,
},
ignorePattern: {
default: [],
transform: asArray,
},
maxWarnings: {
default: -1,
transform: asInt,
},
noEslintrc: {
name: 'useEslintrc',
default: false,
Expand All @@ -60,6 +75,12 @@ const BASE_CONFIG = {
default: [],
transform: asArray,
},
quiet: {
default: false,
},
reportUnusedDisableDirectives: {
default: false,
},
rules: {
default: null,
},
Expand Down

0 comments on commit c811667

Please sign in to comment.