Skip to content

Commit

Permalink
fix: Autogenerate recommended config
Browse files Browse the repository at this point in the history
Merge pull request #4 from nickmccurdy/autogenerate-recommended-config
  • Loading branch information
benmonro authored Oct 31, 2019
2 parents 1051c12 + c6b4899 commit c3e0654
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ Then configure the rules you want to use under the rules section.

```json
{
"rules": {
"jest-dom/prefer-checked": "error",
"jest-dom/prefer-enabled-disabled": "error",
"jest-dom/prefer-required": "error"
}
"rules": {}
}
```

Expand Down
14 changes: 9 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ let requireIndex = require('requireindex');
// import all rules in lib/rules
module.exports.rules = requireIndex(__dirname + '/rules');

module.exports.generateRecommendedConfig = rules => {
return Object.entries(rules).reduce(
(memo, [name, rule]) =>
rule.meta.docs.recommended ? { ...memo, [name]: 'error' } : memo,
{}
);
};

module.exports.configs = {
recommended: {
plugins: ['jest-dom'],
rules: {
'jest-dom/prefer-checked': 'error',
'jest-dom/prefer-enabled-disabled': 'error',
'jest-dom/prefer-required': 'error',
},
rules: module.exports.generateRecommendedConfig(module.exports.rules),
},
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"generate-readme-table": "node build/generate-readme-table.js"
},
"dependencies": {
"requireindex": "~1.1.0"
"requireindex": "~1.2.0"
},
"devDependencies": {
"eslint": "^6.6.0",
Expand All @@ -30,4 +30,4 @@
"node": ">=0.10.0"
},
"license": "ISC"
}
}
41 changes: 40 additions & 1 deletion tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should have all the rules 1`] = `Object {}`;
exports[`should have all the rules 1`] = `
Object {
"prefer-checked": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"category": "jest-dom",
"description": "prefer toBeChecked over checking attributes",
"recommended": true,
"url": "prefer-checked",
},
"fixable": "code",
},
},
"prefer-enabled-disabled": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"category": "jest-dom",
"description": "prefer toBeDisabled or toBeEnabled over checking attributes",
"recommended": true,
"url": "prefer-enabled-disabled",
},
"fixable": "code",
},
},
"prefer-required": Object {
"create": [Function],
"meta": Object {
"docs": Object {
"category": "jest-dom",
"description": "prefer toBeRequired over checking properties",
"recommended": true,
"url": "prefer-required",
},
"fixable": "code",
},
},
}
`;
11 changes: 10 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const { rules } = require('../lib/index');
const { rules, generateRecommendedConfig, configs } = require('../lib/index');

it('should have all the rules', () => {
expect(rules).toMatchSnapshot();
});

it('should have a recommended config with recommended rules', () => {
expect(
generateRecommendedConfig({
good: { meta: { docs: { recommended: true } } },
bad: { meta: { docs: { recommended: false } } },
})
).toEqual({ good: 'error' });
});

0 comments on commit c3e0654

Please sign in to comment.