Skip to content

Improve test coverage of no-identical-tests rule #153

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
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 6 additions & 4 deletions lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ module.exports = {
},
fixable: 'code',
schema: [],
messages: {
identical: 'This test case is identical to another case.',
},
},

create (context) {
// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const message = 'This test case is identical to another case.';
const sourceCode = context.getSourceCode();

// ----------------------------------------------------------------------
Expand All @@ -46,8 +48,8 @@ module.exports = {
return sourceCode.getText(testA) === sourceCode.getText(testB);
}

const propertiesA = testA.properties || [];
const propertiesB = testB.properties || [];
const propertiesA = testA.properties;
const propertiesB = testB.properties;

// if properties length not eq; return false;
if (propertiesA.length !== propertiesB.length) {
Expand Down Expand Up @@ -78,7 +80,7 @@ module.exports = {
if (cache.some(item => eq(item, test))) {
context.report({
node: test,
message,
messageId: 'identical',
fix (fixer) {
const start = sourceCode.getTokenBefore(test);
const end = sourceCode.getTokenAfter(test);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"eslint-utils": "^2.1.0"
},
"nyc": {
"branches": 96,
"branches": 97,
"functions": 98,
"lines": 98,
"lines": 99,
"statements": 98
},
"devDependencies": {
Expand Down
54 changes: 48 additions & 6 deletions tests/lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
const rule = require('../../../lib/rules/no-identical-tests');
const RuleTester = require('eslint').RuleTester;

const ERROR = { message: 'This test case is identical to another case.' };
const ERROR_OBJECT_TEST = { messageId: 'identical', type: 'ObjectExpression' };
const ERROR_STRING_TEST = { messageId: 'identical', type: 'Literal' };

// ------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -46,6 +47,26 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
// Object and string test.
`
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
'foo',
],
invalid: []
});
`,
// One test object with more properties than the other.
`
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo' },
{ code: 'foo', options: [{}] },
],
invalid: []
});
`,
],

invalid: [
Expand All @@ -67,7 +88,7 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
errors: [ERROR],
errors: [ERROR_OBJECT_TEST],
},
{
code: `
Expand All @@ -87,7 +108,7 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
errors: [ERROR],
errors: [ERROR_OBJECT_TEST],
},
{
code: `
Expand All @@ -112,7 +133,7 @@ ruleTester.run('no-identical-tests', rule, {
]
});
`,
errors: [ERROR, ERROR],
errors: [ERROR_OBJECT_TEST, ERROR_OBJECT_TEST],
},
{
code: `
Expand All @@ -132,7 +153,28 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
errors: [ERROR],
errors: [ERROR_OBJECT_TEST],
},
{
// Empty objects.
code: `
new RuleTester().run('foo', bar, {
valid: [
{},
{},
],
invalid: []
});
`,
output: `
new RuleTester().run('foo', bar, {
valid: [
{},
],
invalid: []
});
`,
errors: [ERROR_OBJECT_TEST],
},
{
code: `
Expand All @@ -152,7 +194,7 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
errors: [ERROR],
errors: [ERROR_STRING_TEST],
},
],
});