Skip to content

Commit 8c74f24

Browse files
bmishnot-an-aardvark
authored andcommitted
New: add always option to consistent-output rule (#88)
1 parent 10b28f0 commit 8c74f24

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

docs/rules/consistent-output.md

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ new RuleTester().run('example-rule', rule, {
5656

5757
```
5858

59+
## Options
60+
61+
This rule takes an optional string enum option with one of the following values:
62+
63+
* `"consistent"` - (default) if any invalid test cases have output assertions, then all invalid test cases must have output assertions
64+
* `"always"` - always require invalid test cases to have output assertions
65+
5966
## When Not To Use It
6067

6168
If you're not writing fixable rules, or you want to write test cases without output assertions, do not enable this rule.

lib/rules/consistent-output.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ module.exports = {
2020
},
2121
type: 'suggestion',
2222
fixable: null, // or "code" or "whitespace"
23-
schema: [],
23+
schema: [
24+
{
25+
type: 'string',
26+
enum: ['always', 'consistent'],
27+
},
28+
],
2429
},
2530

2631
create (context) {
2732
// ----------------------------------------------------------------------
2833
// Public
2934
// ----------------------------------------------------------------------
35+
const always = context.options[0] && context.options[0] === 'always';
3036

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

38-
if (casesWithoutOutput.length < readableCases.length) {
44+
if (
45+
(casesWithoutOutput.length < readableCases.length) ||
46+
(always && casesWithoutOutput.length > 0)
47+
) {
3948
casesWithoutOutput.forEach(testCase => {
4049
context.report({
4150
node: testCase,

tests/lib/rules/consistent-output.js

+30
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ ruleTester.run('consistent-output', rule, {
5353
]
5454
});
5555
`,
56+
{
57+
code: `
58+
new RuleTester().run('foo', bar, {
59+
valid: [],
60+
invalid: [
61+
{
62+
code: 'foo',
63+
output: 'baz',
64+
errors: ['bar']
65+
},
66+
]
67+
});
68+
`,
69+
options: ['always'],
70+
},
5671
],
5772

5873
invalid: [
@@ -79,5 +94,20 @@ ruleTester.run('consistent-output', rule, {
7994
`,
8095
errors: [ERROR, ERROR],
8196
},
97+
{
98+
code: `
99+
new RuleTester().run('foo', bar, {
100+
valid: [],
101+
invalid: [
102+
{
103+
code: 'foo',
104+
errors: ['bar'],
105+
},
106+
]
107+
});
108+
`,
109+
options: ['always'],
110+
errors: [ERROR],
111+
},
82112
],
83113
});

0 commit comments

Comments
 (0)