Skip to content

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

Merged
merged 8 commits into from
Jul 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/rules/no-identical-tests.js
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,
Copy link
Contributor

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 to true eventually, but that will be a breaking change, so I think it would be better to set it to false at first and then change it a bit afterwards.

},
fixable: null, // or "code" or "whitespace"
schema: [],
},

create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const message = 'This test case should not be identical to someone else.';
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@aladdin-add aladdin-add Jul 1, 2017

Choose a reason for hiding this comment

The 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;
}
});
});
});
},
};
},
};
64 changes: 64 additions & 0 deletions tests/lib/rules/no-identical-tests.js
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],
},
],
});