Skip to content

Commit

Permalink
Chore: improve test coverage of no-identical-tests rule (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Jul 2, 2021
1 parent c55a956 commit 281d4e5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
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],
},
],
});

0 comments on commit 281d4e5

Please sign in to comment.