-
-
Notifications
You must be signed in to change notification settings - Fork 30
New: disallow identical tests #11
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
Changes from 1 commit
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,53 @@ | ||
/** | ||
* @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: true, | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [], | ||
}, | ||
|
||
create (context) { | ||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
const message = 'This test case should not be identical to someone else.'; | ||
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: Can you reword this message to something like this? "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); | ||
(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 should not be identical to someone else.' }; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// 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.
Can you set this to
false
for now? I agree that it would be good to set it totrue
eventually, but that will be a breaking change, so I think it would be better to set it tofalse
at first and then change it a bit afterwards.