Skip to content

Commit 64ed898

Browse files
New: consistent-output rule
1 parent 667e36f commit 64ed898

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Name | ✔️ | 🛠 | Description
5555
[test-case-shorthand-strings](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/test-case-shorthand-strings.md) | | | Enforces consistent usage of shorthand strings for test cases with no options
5656
[prefer-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/prefer-placeholders.md) | | | Disallows template literals as report messages
5757
[no-useless-token-range](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-useless-token-range.md) | ✔️ | 🛠 | Disallows unnecessary calls to sourceCode.getFirstToken and sourceCode.getLastToken
58+
[consistent-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/consistent-output.md) | | | Enforces consistent use of output assertions in rule tests
5859

5960
## Supported Presets
6061

docs/rules/consistent-output.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Enforce consistent use of output assertions in rule tests (consistent-output)
2+
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.
4+
5+
## Rule Details
6+
7+
This rule aims to ensure that if any invalid test cases have output assertions, then all test cases have output assertions.
8+
9+
Examples of **incorrect** code for this rule:
10+
11+
```js
12+
/* eslint eslint-plugin/consistent-output: error */
13+
14+
new RuleTester().run('example-rule', rule, {
15+
valid: [],
16+
invalid: [
17+
{
18+
code: 'foo',
19+
output: 'bar',
20+
errors: ['baz']
21+
},
22+
{
23+
code: 'bar',
24+
errors: ['baz']
25+
}
26+
]
27+
});
28+
29+
```
30+
31+
Examples of **correct** code for this rule:
32+
33+
```js
34+
/* eslint eslint-plugin/consistent-output: error */
35+
36+
new RuleTester().run('example-rule', rule, {
37+
valid: [],
38+
invalid: [
39+
{
40+
code: 'foo',
41+
output: 'bar',
42+
errors: ['baz']
43+
},
44+
{
45+
code: 'bar',
46+
output: 'qux',
47+
errors: ['baz']
48+
}
49+
]
50+
});
51+
52+
```
53+
54+
## When Not To Use It
55+
56+
If you're not writing fixable rules, or you want to write test cases without output assertions, do not enable this rule.
57+
58+
## Further Reading
59+
60+
* [`RuleTester` documentation](http://eslint.org/docs/developer-guide/working-with-plugins#testing)

lib/rules/consistent-output.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @fileoverview Enforce consistent use of output assertions in rule tests
3+
* @author Teddy Katz
4+
*/
5+
6+
'use strict';
7+
8+
const utils = require('../utils');
9+
10+
// ------------------------------------------------------------------------------
11+
// Rule Definition
12+
// ------------------------------------------------------------------------------
13+
14+
module.exports = {
15+
meta: {
16+
docs: {
17+
description: 'Enforce consistent use of output assertions in rule tests',
18+
category: 'Fill me in',
19+
recommended: false,
20+
},
21+
fixable: null, // or "code" or "whitespace"
22+
schema: [],
23+
},
24+
25+
create (context) {
26+
// ----------------------------------------------------------------------
27+
// Public
28+
// ----------------------------------------------------------------------
29+
30+
return {
31+
Program (ast) {
32+
utils.getTestInfo(context, ast).forEach(testRun => {
33+
if (!testRun.invalid) {
34+
return;
35+
}
36+
const readableCases = testRun.invalid.filter(testCase => testCase.type === 'ObjectExpression');
37+
const casesWithoutOutput = readableCases
38+
.filter(testCase => testCase.properties.map(utils.getKeyName).indexOf('output') === -1);
39+
40+
if (casesWithoutOutput.length < readableCases.length) {
41+
casesWithoutOutput.forEach(testCase => {
42+
context.report({
43+
node: testCase,
44+
message: 'This test case should have an output assertion.',
45+
});
46+
});
47+
}
48+
});
49+
},
50+
};
51+
},
52+
};

tests/lib/rules/consistent-output.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @fileoverview Enforce consistent 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/consistent-output');
13+
const RuleTester = require('eslint').RuleTester;
14+
15+
const ERROR = { message: 'This test case should have an output assertion.', type: 'ObjectExpression' };
16+
17+
// ------------------------------------------------------------------------------
18+
// Tests
19+
// ------------------------------------------------------------------------------
20+
21+
const ruleTester = new RuleTester();
22+
ruleTester.run('consistent-output', rule, {
23+
valid: [
24+
`
25+
new RuleTester().run('foo', bar, {
26+
valid: [],
27+
invalid: [
28+
{
29+
code: 'foo',
30+
errors: ['bar']
31+
},
32+
{
33+
code: 'baz',
34+
errors: ['qux']
35+
}
36+
]
37+
});
38+
`,
39+
`
40+
new RuleTester().run('foo', bar, {
41+
valid: [],
42+
invalid: [
43+
{
44+
code: 'foo',
45+
output: 'baz',
46+
errors: ['bar'],
47+
},
48+
{
49+
code: 'foo',
50+
output: 'qux',
51+
errors: ['bar']
52+
}
53+
]
54+
});
55+
`,
56+
],
57+
58+
invalid: [
59+
{
60+
code: `
61+
new RuleTester().run('foo', bar, {
62+
valid: [],
63+
invalid: [
64+
{
65+
code: 'foo',
66+
output: 'baz',
67+
errors: ['bar'],
68+
},
69+
{
70+
code: 'foo',
71+
errors: ['bar']
72+
},
73+
{
74+
code: 'foo bar',
75+
errors: ['bar']
76+
}
77+
]
78+
});
79+
`,
80+
errors: [ERROR, ERROR],
81+
},
82+
],
83+
});

0 commit comments

Comments
 (0)