-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New: disallow identical tests #11
Changes from 7 commits
3a6811b
2dd49d4
409e5aa
41c8252
83610cd
642802c
c58b8b9
d3f2f32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Disallow identical tests (no-identical-tests) | ||
|
||
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. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/consistent-output: error */ | ||
|
||
new RuleTester().run('foo', bar, { | ||
valid: [ | ||
{ code: 'foo' }, | ||
{ code: 'foo' } | ||
], | ||
invalid: [] | ||
}); | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/consistent-output: error */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: This should say |
||
|
||
new RuleTester().run('foo', bar, { | ||
valid: [ | ||
{ code: 'foo' }, | ||
{ code: 'bar' } | ||
], | ||
invalid: [] | ||
}); | ||
|
||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you want to allow identical tests, do not enable this rule. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @fileoverview disallow identical tests | ||
* @author 薛定谔的猫<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow identical tests', | ||
category: 'Tests', | ||
recommended: false, | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [], | ||
}, | ||
|
||
create (context) { | ||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
const message = 'This test case is identical to another case.'; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
Program (ast) { | ||
utils.getTestInfo(context, ast).forEach(testRun => { | ||
[testRun.valid, testRun.invalid].forEach(tests => { | ||
const cache = Object.create(null); | ||
// to avoid tests being null | ||
(tests || []).forEach(test => { | ||
const testCode = sourceCode.getText(test); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually, I think it would also be useful to also catch cases like this: {
code: 'foo',
options: ['bar']
},
{
options: ['bar'],
code: 'foo'
} But it's probably not necessary to change that as part of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it is an enhancement, I will finish it in another PR.:( |
||
if (cache[testCode]) { | ||
context.report({ | ||
node: test, | ||
message, | ||
}); | ||
} else { | ||
cache[testCode] = true; | ||
} | ||
}); | ||
}); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @fileoverview disallow identical tests | ||
* @author 薛定谔的猫<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-identical-tests'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const ERROR = { message: 'This test case is identical to another case.' }; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run('no-identical-tests', rule, { | ||
valid: [ | ||
` | ||
new RuleTester().run('foo', bar, { | ||
valid: [ | ||
{ code: 'foo' }, | ||
{ code: 'bar' } | ||
], | ||
invalid: [] | ||
}); | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
new RuleTester().run('foo', bar, { | ||
valid: [ | ||
{ code: 'foo' }, | ||
{ code: 'foo' } | ||
], | ||
invalid: [] | ||
}); | ||
`, | ||
errors: [ERROR], | ||
}, | ||
{ | ||
code: ` | ||
new RuleTester().run('foo', bar, { | ||
valid: [ | ||
{ code: 'foo' }, | ||
{ code: 'foo' }, | ||
], | ||
invalid: [ | ||
{ code: 'foo', errors: ['bar'] }, | ||
{ code: 'foo', errors: ['bar'] }, | ||
] | ||
}); | ||
`, | ||
errors: [ERROR, ERROR], | ||
}, | ||
], | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: This should say
/* eslint eslint-plugin/no-identical-tests: error */