Skip to content

Commit 9e4c37a

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 9e4c37a

File tree

5 files changed

+142
-133
lines changed

5 files changed

+142
-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.
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @fileoverview Enforce use of `output` assertions in rule tests
3+
* @author Teddy Katz
4+
*/
5+
6+
'use strict';
7+
8+
// ------------------------------------------------------------------------------
9+
// Requirements
10+
// ------------------------------------------------------------------------------
11+
12+
const rule = require('../../../lib/rules/require-test-output');
13+
const RuleTester = require('eslint').RuleTester;
14+
15+
const ERROR = { messageId: 'missingOutput', type: 'ObjectExpression' };
16+
17+
// ------------------------------------------------------------------------------
18+
// Tests
19+
// ------------------------------------------------------------------------------
20+
21+
const ruleTester = new RuleTester();
22+
ruleTester.run('require-test-output', rule, {
23+
valid: [
24+
{
25+
// Explicit option of `consistent` (no output in any tests).
26+
code: `
27+
new RuleTester().run('foo', bar, {
28+
valid: [],
29+
invalid: [
30+
{ code: 'foo', errors: ['bar'] },
31+
{ code: 'baz', errors: ['qux'] }
32+
]
33+
});
34+
`,
35+
options: [{ consistent: true }],
36+
},
37+
{
38+
// Explicit option of `consistent` (output in all tests).
39+
code: `
40+
new RuleTester().run('foo', bar, {
41+
valid: [],
42+
invalid: [
43+
{ code: 'foo', output: 'baz', errors: ['bar'] },
44+
{ code: 'foo', output: 'qux', errors: ['bar'] },
45+
]
46+
});
47+
`,
48+
options: [{ consistent: true }],
49+
},
50+
{
51+
// Explicitly turning off the `consistent` option (results in "always" behavior).
52+
code: `
53+
new RuleTester().run('foo', bar, {
54+
valid: [],
55+
invalid: [
56+
{ code: 'foo', output: 'baz', errors: ['bar'] },
57+
{ code: 'foo', output: 'qux', errors: ['bar'] },
58+
{ code: 'foo', output: null, errors: ['bar'] },
59+
]
60+
});
61+
`,
62+
options: [{ consistent: false }],
63+
},
64+
// When defaulting to the "always" behavior.
65+
`
66+
new RuleTester().run('foo', bar, {
67+
valid: [],
68+
invalid: [
69+
{ code: 'foo', output: 'baz', errors: ['bar'] },
70+
]
71+
});
72+
`,
73+
],
74+
75+
invalid: [
76+
{
77+
// Explicit option of `consistent`.
78+
code: `
79+
new RuleTester().run('foo', bar, {
80+
valid: [],
81+
invalid: [
82+
{ code: 'foo', output: 'baz', errors: ['bar'] },
83+
{ code: 'foo', errors: ['bar'] },
84+
{ code: 'foo bar', errors: ['bar'] },
85+
]
86+
});
87+
`,
88+
options: [{ consistent: true }],
89+
errors: [ERROR, ERROR],
90+
},
91+
{
92+
// Explicitly turning off the `consistent` option (results in "always" behavior).
93+
code: `
94+
new RuleTester().run('foo', bar, {
95+
valid: [],
96+
invalid: [
97+
{ code: 'foo', errors: ['bar'] },
98+
]
99+
});
100+
`,
101+
options: [{ consistent: false }],
102+
errors: [ERROR],
103+
},
104+
{
105+
// When defaulting to the "always" behavior.
106+
code: `
107+
new RuleTester().run('foo', bar, {
108+
valid: [],
109+
invalid: [
110+
{ code: 'foo', errors: ['bar'] },
111+
]
112+
});
113+
`,
114+
errors: [ERROR],
115+
},
116+
],
117+
});

0 commit comments

Comments
 (0)