Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow config overrides #13

Merged
merged 8 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/node_modules
build

**/coverage/
integrationTests/__fixtures__/skipped/__eslint__/file.js
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,55 @@ module.exports = {
```bash
yarn jest
```


## Options

This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig), so you can provide config via:
* a `jest-runner-eslint` property in your `package.json`
* a `jest-runner-eslint.config.js` JS file
* a `.jest-runner-eslintrc` JSON file


In `package.json`
```json
{
"jest-runner-eslint": {
"cliOptions": {
// Options here
}
}
}
```

or in `jest-runner-eslint.config.js`
```js
module.exports = {
cliOptions: {
// Options here
}
}
```


### cliOptions

jest-runner-eslint maps a lot of ESLint CLI arguments to config options. For example `--fix` is `cliOptions.fix`

|option|default|example
|-----|-----|-----|
|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`
|global|`[]`|`"global": "it"` or `"global": ["it", "describe"]`
|ignorePath|`null`|`"ignorePath": "/path/to/ignore"`
|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"]`
|rule|`null`|`"rule": "'quotes: [2, double]'"` or `"rule": ["quotes: [2, double]", "no-console: 2"]`
|rulesdir|`[]`|`"rulesdir": "/path/to/rules/dir"` or `"env": ["/path/to/rules/dir", "/path/to/other"]`
1 change: 0 additions & 1 deletion integrationTests/__fixtures__/passing/__eslint__/file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global hello */
const a = 1;

if (a === 2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
cliOptions: {
global: ['hello'],
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"format": "prettier --single-quote --trailing-comma all --write \"!(build)/**/*.js\""
},
"dependencies": {
"cosmiconfig": "^3.0.1",
"eslint": "4.5.0",
"find-up": "^2.1.0",
"pify": "3.0.0",
Expand Down
9 changes: 8 additions & 1 deletion src/runESLint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const getLocalESLint = require('./utils/getLocalESLint');
const getESLintOptions = require('./utils/getESLintOptions');
const toTestResult = require('./utils/toTestResult');

const skip = ({ start, end, testPath }) =>
Expand Down Expand Up @@ -62,12 +63,18 @@ const runESLint = ({ testPath, config }, workerCallback) => {
try {
const start = +new Date();
const { CLIEngine } = getLocalESLint(config);
const cli = new CLIEngine({});
const options = getESLintOptions(config);
const cli = new CLIEngine(options.cliOptions);
if (cli.isPathIgnored(testPath)) {
const end = +new Date();
workerCallback(null, skip({ start, end, testPath }));
} else {
const report = cli.executeOnFiles([testPath]);

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

const end = +new Date();

if (report.errorCount > 0) {
Expand Down
194 changes: 194 additions & 0 deletions src/utils/__tests__/normalizeConfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
const normalizeConfig = require('../normalizeConfig');

const normalizeCLIOptions = cliOptions =>
normalizeConfig({ cliOptions }).cliOptions;

it('ignores unkown options', () => {
expect(normalizeCLIOptions({ other: true })).not.toMatchObject({
other: true,
});
});

it('normalizes noInlineConfig', () => {
expect(normalizeCLIOptions({})).toMatchObject({
allowInlineConfig: true,
});

expect(normalizeCLIOptions({ noInlineConfig: true })).toMatchObject({
allowInlineConfig: false,
});

expect(normalizeCLIOptions({ noInlineConfig: false })).toMatchObject({
allowInlineConfig: true,
});
});

it('normalizes cacheLocation', () => {
expect(normalizeCLIOptions({})).toMatchObject({
cacheLocation: '.eslintcache',
});

expect(
normalizeCLIOptions({ cacheLocation: '/path/to/cache' }),
).toMatchObject({
cacheLocation: '/path/to/cache',
});
});

it('normalizes config', () => {
expect(normalizeCLIOptions({})).toMatchObject({
configFile: null,
});

expect(normalizeCLIOptions({ config: '/path/to/config' })).toMatchObject({
configFile: '/path/to/config',
});
});

it('normalizes env', () => {
expect(normalizeCLIOptions({})).toMatchObject({
envs: [],
});

expect(normalizeCLIOptions({ env: 'mocha' })).toMatchObject({
envs: ['mocha'],
});

expect(normalizeCLIOptions({ env: ['mocha', 'browser'] })).toMatchObject({
envs: ['mocha', 'browser'],
});
});

it('normalizes ext', () => {
expect(normalizeCLIOptions({})).toMatchObject({
extensions: ['.js'],
});

expect(normalizeCLIOptions({ ext: '.ts' })).toMatchObject({
extensions: ['.ts'],
});

expect(normalizeCLIOptions({ ext: ['.js', '.jsx', '.ts'] })).toMatchObject({
extensions: ['.js', '.jsx', '.ts'],
});
});

it('normalizes fix', () => {
expect(normalizeCLIOptions({})).toMatchObject({
fix: false,
});

expect(normalizeCLIOptions({ fix: true })).toMatchObject({
fix: true,
});
});

it('normalizes global', () => {
expect(normalizeCLIOptions({})).toMatchObject({
globals: [],
});

expect(normalizeCLIOptions({ global: 'it' })).toMatchObject({
globals: ['it'],
});

expect(normalizeCLIOptions({ global: ['it', 'describe'] })).toMatchObject({
globals: ['it', 'describe'],
});
});

it('normalizes noIgnore', () => {
expect(normalizeCLIOptions({})).toMatchObject({
ignore: true,
});

expect(normalizeCLIOptions({ noIgnore: true })).toMatchObject({
ignore: false,
});
});

it('normalizes ignorePath', () => {
expect(normalizeCLIOptions({})).toMatchObject({
ignorePath: null,
});

expect(normalizeCLIOptions({ ignorePath: '/path/to/ignore' })).toMatchObject({
ignorePath: '/path/to/ignore',
});
});

it('normalizes parser', () => {
expect(normalizeCLIOptions({})).toMatchObject({
parser: 'espree',
});

expect(normalizeCLIOptions({ parser: 'flow' })).toMatchObject({
parser: 'flow',
});
});

it('normalizes parserOptions', () => {
expect(normalizeCLIOptions({})).toMatchObject({
parserOptions: {},
});

expect(
normalizeCLIOptions({ parserOptions: { ecmaVersion: 2015 } }),
).toMatchObject({
parserOptions: { ecmaVersion: 2015 },
});
});

it('normalizes plugin', () => {
expect(normalizeCLIOptions({})).toMatchObject({
plugins: [],
});

expect(normalizeCLIOptions({ plugin: 'prettier' })).toMatchObject({
plugins: ['prettier'],
});

expect(normalizeCLIOptions({ plugin: ['prettier'] })).toMatchObject({
plugins: ['prettier'],
});
});

it('normalizes rulesdir', () => {
expect(normalizeCLIOptions({})).toMatchObject({
rulePaths: [],
});

expect(normalizeCLIOptions({ rulesdir: '/path/to/rules' })).toMatchObject({
rulePaths: ['/path/to/rules'],
});

expect(
normalizeCLIOptions({ rulesdir: ['/path/to/rules', '/other/path'] }),
).toMatchObject({
rulePaths: ['/path/to/rules', '/other/path'],
});
});

it('normalizes rule', () => {
expect(normalizeCLIOptions({})).toMatchObject({
rules: null,
});

expect(normalizeCLIOptions({ rule: ['quotes: [2, double]'] })).toMatchObject({
rules: ['quotes: [2, double]'],
});

expect(normalizeCLIOptions({ rule: 'quotes: [2, double]' })).toMatchObject({
rules: ['quotes: [2, double]'],
});
});

it('normalizes noEslintrc', () => {
expect(normalizeCLIOptions({})).toMatchObject({
useEslintrc: true,
});

expect(normalizeCLIOptions({ noEslintrc: true })).toMatchObject({
useEslintrc: false,
});
});
16 changes: 16 additions & 0 deletions src/utils/getESLintOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const normalizeConfig = require('./normalizeConfig');
const cosmiconfig = require('cosmiconfig');

const explorer = cosmiconfig('jest-runner-eslint', { sync: true });

const getESLintOptions = config => {
const result = explorer.load(config.rootDir);

if (result) {
return normalizeConfig(result.config);
}

return normalizeConfig({});
};

module.exports = getESLintOptions;
Loading