Skip to content

Add always option to consistent-output rule #88

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

Merged
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
7 changes: 7 additions & 0 deletions docs/rules/consistent-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ new RuleTester().run('example-rule', rule, {

```

## Options

This rule takes an optional string enum option with one of the following values:

* `"consistent"` - (default) if any invalid test cases have output assertions, then all invalid test cases must have output assertions
* `"always"` - always require invalid test cases to have output assertions

## When Not To Use It

If you're not writing fixable rules, or you want to write test cases without output assertions, do not enable this rule.
Expand Down
13 changes: 11 additions & 2 deletions lib/rules/consistent-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ module.exports = {
},
type: 'suggestion',
fixable: null, // or "code" or "whitespace"
schema: [],
schema: [
{
type: 'string',
enum: ['always', 'consistent'],
},
],
},

create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const always = context.options[0] && context.options[0] === 'always';

return {
Program (ast) {
Expand All @@ -35,7 +41,10 @@ module.exports = {
const casesWithoutOutput = readableCases
.filter(testCase => testCase.properties.map(utils.getKeyName).indexOf('output') === -1);

if (casesWithoutOutput.length < readableCases.length) {
if (
(casesWithoutOutput.length < readableCases.length) ||
(always && casesWithoutOutput.length > 0)
) {
casesWithoutOutput.forEach(testCase => {
context.report({
node: testCase,
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/consistent-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ ruleTester.run('consistent-output', rule, {
]
});
`,
{
code: `
new RuleTester().run('foo', bar, {
valid: [],
invalid: [
{
code: 'foo',
output: 'baz',
errors: ['bar']
},
]
});
`,
options: ['always'],
},
],

invalid: [
Expand All @@ -79,5 +94,20 @@ ruleTester.run('consistent-output', rule, {
`,
errors: [ERROR, ERROR],
},
{
code: `
new RuleTester().run('foo', bar, {
valid: [],
invalid: [
{
code: 'foo',
errors: ['bar'],
},
]
});
`,
options: ['always'],
errors: [ERROR],
},
],
});