Skip to content

Commit 8c19ab5

Browse files
aladdin-addnot-an-aardvark
authored andcommitted
New: no-identical-tests rule (#11)
1 parent 58574ff commit 8c19ab5

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Name | ✔️ | 🛠 | Description
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
5858
[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
59+
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | | Disallows identical tests
5960

6061
## Supported Presets
6162

docs/rules/no-identical-tests.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Disallow identical tests (no-identical-tests)
2+
3+
When a rule has a lot of tests, it's sometimes difficult to tell if any tests are duplicates. This rule would warn if any test cases have the same properties.
4+
5+
## Rule Details
6+
7+
Examples of **incorrect** code for this rule:
8+
9+
```js
10+
/* eslint eslint-plugin/no-identical-tests: error */
11+
12+
new RuleTester().run('foo', bar, {
13+
valid: [
14+
{ code: 'foo' },
15+
{ code: 'foo' }
16+
],
17+
invalid: []
18+
});
19+
20+
```
21+
22+
Examples of **correct** code for this rule:
23+
24+
```js
25+
/* eslint eslint-plugin/no-identical-tests: error */
26+
27+
new RuleTester().run('foo', bar, {
28+
valid: [
29+
{ code: 'foo' },
30+
{ code: 'bar' }
31+
],
32+
invalid: []
33+
});
34+
35+
```
36+
37+
## When Not To Use It
38+
39+
If you want to allow identical tests, do not enable this rule.

lib/rules/no-identical-tests.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @fileoverview disallow identical tests
3+
* @author 薛定谔的猫<[email protected]>
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: 'disallow identical tests',
18+
category: 'Tests',
19+
recommended: false,
20+
},
21+
fixable: null, // or "code" or "whitespace"
22+
schema: [],
23+
},
24+
25+
create (context) {
26+
// ----------------------------------------------------------------------
27+
// Public
28+
// ----------------------------------------------------------------------
29+
const message = 'This test case is identical to another case.';
30+
const sourceCode = context.getSourceCode();
31+
32+
return {
33+
Program (ast) {
34+
utils.getTestInfo(context, ast).forEach(testRun => {
35+
[testRun.valid, testRun.invalid].forEach(tests => {
36+
const cache = Object.create(null);
37+
// to avoid tests being null
38+
(tests || []).forEach(test => {
39+
const testCode = sourceCode.getText(test);
40+
if (cache[testCode]) {
41+
context.report({
42+
node: test,
43+
message,
44+
});
45+
} else {
46+
cache[testCode] = true;
47+
}
48+
});
49+
});
50+
});
51+
},
52+
};
53+
},
54+
};

tests/lib/rules/no-identical-tests.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileoverview disallow identical tests
3+
* @author 薛定谔的猫<[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
// ------------------------------------------------------------------------------
9+
// Requirements
10+
// ------------------------------------------------------------------------------
11+
12+
const rule = require('../../../lib/rules/no-identical-tests');
13+
const RuleTester = require('eslint').RuleTester;
14+
15+
const ERROR = { message: 'This test case is identical to another case.' };
16+
17+
// ------------------------------------------------------------------------------
18+
// Tests
19+
// ------------------------------------------------------------------------------
20+
21+
const ruleTester = new RuleTester();
22+
ruleTester.run('no-identical-tests', rule, {
23+
valid: [
24+
`
25+
new RuleTester().run('foo', bar, {
26+
valid: [
27+
{ code: 'foo' },
28+
{ code: 'bar' }
29+
],
30+
invalid: []
31+
});
32+
`,
33+
],
34+
35+
invalid: [
36+
{
37+
code: `
38+
new RuleTester().run('foo', bar, {
39+
valid: [
40+
{ code: 'foo' },
41+
{ code: 'foo' }
42+
],
43+
invalid: []
44+
});
45+
`,
46+
errors: [ERROR],
47+
},
48+
{
49+
code: `
50+
new RuleTester().run('foo', bar, {
51+
valid: [
52+
{ code: 'foo' },
53+
{ code: 'foo' },
54+
],
55+
invalid: [
56+
{ code: 'foo', errors: ['bar'] },
57+
{ code: 'foo', errors: ['bar'] },
58+
]
59+
});
60+
`,
61+
errors: [ERROR, ERROR],
62+
},
63+
],
64+
});

0 commit comments

Comments
 (0)