Skip to content

Commit 46f9918

Browse files
committed
breaking: rename consistent-output rule to require-test-output
Changes the default rule behavior to always require a test `output` assertion. The old default behavior of `consistent` (only require `output` assertions if some tests have it) is still provided as an available option. * [As of ESLint 7](https://eslint.org/docs/user-guide/migrating-to-7.0.0#additional-validation-added-to-the-ruletester-class), the `output` property is required for test cases that provide an autofix * And even when test cases don't autofix, it's still best practice to assert that there's no autofix with `output: null`
1 parent cd65a5c commit 46f9918

13 files changed

+193
-133
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ Then configure the rules you want to use under the rules section.
4747
<!-- __BEGIN AUTOGENERATED TABLE__ -->
4848
Name | ✔️ | 🛠 | 💡 | Description
4949
----- | ----- | ----- | ----- | -----
50-
[consistent-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/consistent-output.md) | | | | enforce consistent use of output assertions in rule tests
5150
[fixer-return](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/fixer-return.md) | ✔️ | | | require fixer function to return a fix
5251
[meta-property-ordering](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/meta-property-ordering.md) | | 🛠 | | enforce the order of meta properties
5352
[no-deprecated-context-methods](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-deprecated-context-methods.md) | | 🛠 | | disallow usage of deprecated methods on rule context objects
@@ -68,6 +67,7 @@ Name | ✔️ | 🛠 | 💡 | Description
6867
[require-meta-has-suggestions](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-has-suggestions.md) | | | | require suggestable rules to implement a `meta.hasSuggestions` property
6968
[require-meta-schema](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-schema.md) | | 🛠 | | require rules to implement a meta.schema property
7069
[require-meta-type](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-type.md) | | | | require rules to implement a meta.type property
70+
[require-test-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-test-output.md) | | | | enforce use of `output` assertions in rule tests
7171
[test-case-property-ordering](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-property-ordering.md) | | 🛠 | | require the properties of a test case to be placed in a consistent order
7272
[test-case-shorthand-strings](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-shorthand-strings.md) | | 🛠 | | enforce consistent usage of shorthand strings for test cases with no options
7373
<!-- __END AUTOGENERATED TABLE__ -->

docs/rules/consistent-output.md renamed to docs/rules/require-test-output.md

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# Enforce consistent use of output assertions in rule tests (consistent-output)
1+
# Enforce use of `output` assertions in rule tests (require-test-output)
22

3-
When writing tests for a fixable rule with `RuleTester`, you can assert the autofix output of your test cases. However, it can be easy to forget to assert the output of a particular test case.
3+
When writing tests for a fixable rule with `RuleTester`, you can assert the autofix `output` of your test cases.
44

55
[As of ESLint 7](https://eslint.org/docs/user-guide/migrating-to-7.0.0#additional-validation-added-to-the-ruletester-class), test cases that trigger an autofix are required to provide the `output` property.
66

7-
Even test that do not trigger an autofix can benefit from asserting that they have no autofix using `output: null`.
7+
However, it can be easy to forget to assert the output of a particular test case, leading to the autofix being untested. And even tests that do not trigger an autofix can benefit from asserting that they have no autofix using `output: null`.
88

99
## Rule Details
1010

11-
This rule aims to ensure that if any invalid test cases have output assertions, then all test cases have output assertions.
11+
This rule ensures all invalid test cases have `output` assertions.
1212

1313
Examples of **incorrect** code for this rule:
1414

1515
```js
16-
/* eslint eslint-plugin/consistent-output: error */
16+
/* eslint eslint-plugin/require-test-output: error */
1717

1818
new RuleTester().run('example-rule', rule, {
1919
valid: [],
@@ -34,7 +34,7 @@ new RuleTester().run('example-rule', rule, {
3434
Examples of **correct** code for this rule:
3535

3636
```js
37-
/* eslint eslint-plugin/consistent-output: error */
37+
/* eslint eslint-plugin/require-test-output: error */
3838

3939
new RuleTester().run('example-rule', rule, {
4040
valid: [],
@@ -60,14 +60,9 @@ new RuleTester().run('example-rule', rule, {
6060

6161
## Options
6262

63-
This rule takes an optional string enum option with one of the following values:
63+
This rule takes an optional object containing:
6464

65-
* `"consistent"` - (default) if any invalid test cases have output assertions, then all invalid test cases must have output assertions
66-
* `"always"` - always require invalid test cases to have output assertions
67-
68-
## When Not To Use It
69-
70-
If you're not writing fixable rules, or you want to write test cases without output assertions, do not enable this rule.
65+
* `boolean` -- `consistent` -- only require `output` assertions when some test cases have them (default `false`)
7166

7267
## Further Reading
7368

lib/rules/consistent-output.js renamed to lib/rules/require-test-output.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Enforce consistent use of output assertions in rule tests
2+
* @fileoverview Enforce use of `output` assertions in rule tests
33
* @author Teddy Katz
44
*/
55

@@ -15,24 +15,34 @@ module.exports = {
1515
meta: {
1616
type: 'suggestion',
1717
docs: {
18-
description: 'enforce consistent use of output assertions in rule tests',
18+
description: 'enforce use of `output` assertions in rule tests',
1919
category: 'Tests',
2020
recommended: false,
2121
},
2222
fixable: null, // or "code" or "whitespace"
2323
schema: [
2424
{
25-
type: 'string',
26-
enum: ['always', 'consistent'],
25+
type: 'object',
26+
properties: {
27+
consistent: {
28+
type: 'boolean',
29+
default: false,
30+
},
31+
},
32+
additionalProperties: false,
2733
},
2834
],
35+
messages: {
36+
missingOutput: 'This test case should have an `output` assertion.',
37+
},
2938
},
3039

3140
create (context) {
3241
// ----------------------------------------------------------------------
3342
// Public
3443
// ----------------------------------------------------------------------
35-
const always = context.options[0] && context.options[0] === 'always';
44+
const consistent = context.options[0] && context.options[0].consistent;
45+
const always = !consistent;
3646

3747
return {
3848
Program (ast) {
@@ -48,7 +58,7 @@ module.exports = {
4858
casesWithoutOutput.forEach(testCase => {
4959
context.report({
5060
node: testCase,
51-
message: 'This test case should have an output assertion.',
61+
messageId: 'missingOutput',
5262
});
5363
});
5464
}

tests/lib/rules/consistent-output.js

-113
This file was deleted.

tests/lib/rules/fixer-return.js

+12
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ ruleTester.run('fixer-return', rule, {
237237
}
238238
};
239239
`,
240+
output: null,
240241
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 24 }],
241242
},
242243
{
@@ -252,6 +253,7 @@ ruleTester.run('fixer-return', rule, {
252253
}
253254
};
254255
`,
256+
output: null,
255257
errors: [{ messageId: 'missingFix', type: 'ArrowFunctionExpression', line: 5, endLine: 5, column: 34, endColumn: 36 }],
256258
},
257259
{
@@ -267,6 +269,7 @@ ruleTester.run('fixer-return', rule, {
267269
}
268270
};
269271
`,
272+
output: null,
270273
errors: [{ messageId: 'missingFix', type: 'ArrowFunctionExpression', line: 5, endLine: 5, column: 34, endColumn: 36 }],
271274
},
272275
{
@@ -280,6 +283,7 @@ ruleTester.run('fixer-return', rule, {
280283
}
281284
};
282285
`,
286+
output: null,
283287
errors: [{ messageId: 'missingFix', type: 'ArrowFunctionExpression', line: 5, endLine: 5, column: 32, endColumn: 34 }],
284288
},
285289
{
@@ -295,6 +299,7 @@ ruleTester.run('fixer-return', rule, {
295299
}
296300
};
297301
`,
302+
output: null,
298303
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 25 }],
299304
},
300305
{
@@ -310,6 +315,7 @@ ruleTester.run('fixer-return', rule, {
310315
}
311316
};
312317
`,
318+
output: null,
313319
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 25 }],
314320
},
315321
{
@@ -325,6 +331,7 @@ ruleTester.run('fixer-return', rule, {
325331
}
326332
};
327333
`,
334+
output: null,
328335
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 26 }],
329336
},
330337
{
@@ -340,6 +347,7 @@ ruleTester.run('fixer-return', rule, {
340347
}
341348
};
342349
`,
350+
output: null,
343351
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 26 }],
344352
},
345353
{
@@ -356,6 +364,7 @@ ruleTester.run('fixer-return', rule, {
356364
}
357365
};
358366
`,
367+
output: null,
359368
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 26 }],
360369
},
361370
{
@@ -371,6 +380,7 @@ ruleTester.run('fixer-return', rule, {
371380
}
372381
};
373382
`,
383+
output: null,
374384
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 26 }],
375385
},
376386
{
@@ -386,6 +396,7 @@ ruleTester.run('fixer-return', rule, {
386396
}
387397
};
388398
`,
399+
output: null,
389400
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 26 }],
390401
},
391402
{
@@ -400,6 +411,7 @@ ruleTester.run('fixer-return', rule, {
400411
}
401412
};
402413
`,
414+
output: null,
403415
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 26 }],
404416
},
405417
],

tests/lib/rules/no-missing-placeholders.js

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ ruleTester.run('no-missing-placeholders', rule, {
142142
}
143143
};
144144
`,
145+
output: null,
145146
errors: [error('bar')],
146147
},
147148
{
@@ -156,6 +157,7 @@ ruleTester.run('no-missing-placeholders', rule, {
156157
}
157158
};
158159
`,
160+
output: null,
159161
errors: [error('bar')],
160162
},
161163
{
@@ -170,6 +172,7 @@ ruleTester.run('no-missing-placeholders', rule, {
170172
}
171173
};
172174
`,
175+
output: null,
173176
errors: [error('hasOwnProperty')],
174177
},
175178
{
@@ -178,6 +181,7 @@ ruleTester.run('no-missing-placeholders', rule, {
178181
context.report(node, 'foo {{bar}}', { baz: 'qux' });
179182
};
180183
`,
184+
output: null,
181185
errors: [error('bar')],
182186
},
183187
{
@@ -188,6 +192,7 @@ ruleTester.run('no-missing-placeholders', rule, {
188192
context.report(node, MESSAGE, { baz: 'qux' });
189193
};
190194
`,
195+
output: null,
191196
errors: [error('bar', 'Identifier')],
192197
},
193198
{
@@ -196,6 +201,7 @@ ruleTester.run('no-missing-placeholders', rule, {
196201
context.report(node, { line: 1, column: 3 }, 'foo {{bar}}', { baz: 'baz' });
197202
};
198203
`,
204+
output: null,
199205
errors: [error('bar')],
200206
},
201207
{
@@ -210,6 +216,7 @@ ruleTester.run('no-missing-placeholders', rule, {
210216
}
211217
};
212218
`,
219+
output: null,
213220
errors: [error('bar')],
214221
},
215222
],

0 commit comments

Comments
 (0)