Skip to content

Commit 281d4e5

Browse files
authored
Chore: improve test coverage of no-identical-tests rule (#153)
1 parent c55a956 commit 281d4e5

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

lib/rules/no-identical-tests.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ module.exports = {
2121
},
2222
fixable: 'code',
2323
schema: [],
24+
messages: {
25+
identical: 'This test case is identical to another case.',
26+
},
2427
},
2528

2629
create (context) {
2730
// ----------------------------------------------------------------------
2831
// Public
2932
// ----------------------------------------------------------------------
30-
const message = 'This test case is identical to another case.';
3133
const sourceCode = context.getSourceCode();
3234

3335
// ----------------------------------------------------------------------
@@ -46,8 +48,8 @@ module.exports = {
4648
return sourceCode.getText(testA) === sourceCode.getText(testB);
4749
}
4850

49-
const propertiesA = testA.properties || [];
50-
const propertiesB = testB.properties || [];
51+
const propertiesA = testA.properties;
52+
const propertiesB = testB.properties;
5153

5254
// if properties length not eq; return false;
5355
if (propertiesA.length !== propertiesB.length) {
@@ -78,7 +80,7 @@ module.exports = {
7880
if (cache.some(item => eq(item, test))) {
7981
context.report({
8082
node: test,
81-
message,
83+
messageId: 'identical',
8284
fix (fixer) {
8385
const start = sourceCode.getTokenBefore(test);
8486
const end = sourceCode.getTokenAfter(test);

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"eslint-utils": "^2.1.0"
3434
},
3535
"nyc": {
36-
"branches": 96,
36+
"branches": 97,
3737
"functions": 98,
38-
"lines": 98,
38+
"lines": 99,
3939
"statements": 98
4040
},
4141
"devDependencies": {

tests/lib/rules/no-identical-tests.js

+48-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
const rule = require('../../../lib/rules/no-identical-tests');
1313
const RuleTester = require('eslint').RuleTester;
1414

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

1718
// ------------------------------------------------------------------------------
1819
// Tests
@@ -46,6 +47,26 @@ ruleTester.run('no-identical-tests', rule, {
4647
invalid: []
4748
});
4849
`,
50+
// Object and string test.
51+
`
52+
new RuleTester().run('foo', bar, {
53+
valid: [
54+
{ code: 'foo' },
55+
'foo',
56+
],
57+
invalid: []
58+
});
59+
`,
60+
// One test object with more properties than the other.
61+
`
62+
new RuleTester().run('foo', bar, {
63+
valid: [
64+
{ code: 'foo' },
65+
{ code: 'foo', options: [{}] },
66+
],
67+
invalid: []
68+
});
69+
`,
4970
],
5071

5172
invalid: [
@@ -67,7 +88,7 @@ ruleTester.run('no-identical-tests', rule, {
6788
invalid: []
6889
});
6990
`,
70-
errors: [ERROR],
91+
errors: [ERROR_OBJECT_TEST],
7192
},
7293
{
7394
code: `
@@ -87,7 +108,7 @@ ruleTester.run('no-identical-tests', rule, {
87108
invalid: []
88109
});
89110
`,
90-
errors: [ERROR],
111+
errors: [ERROR_OBJECT_TEST],
91112
},
92113
{
93114
code: `
@@ -112,7 +133,7 @@ ruleTester.run('no-identical-tests', rule, {
112133
]
113134
});
114135
`,
115-
errors: [ERROR, ERROR],
136+
errors: [ERROR_OBJECT_TEST, ERROR_OBJECT_TEST],
116137
},
117138
{
118139
code: `
@@ -132,7 +153,28 @@ ruleTester.run('no-identical-tests', rule, {
132153
invalid: []
133154
});
134155
`,
135-
errors: [ERROR],
156+
errors: [ERROR_OBJECT_TEST],
157+
},
158+
{
159+
// Empty objects.
160+
code: `
161+
new RuleTester().run('foo', bar, {
162+
valid: [
163+
{},
164+
{},
165+
],
166+
invalid: []
167+
});
168+
`,
169+
output: `
170+
new RuleTester().run('foo', bar, {
171+
valid: [
172+
{},
173+
],
174+
invalid: []
175+
});
176+
`,
177+
errors: [ERROR_OBJECT_TEST],
136178
},
137179
{
138180
code: `
@@ -152,7 +194,7 @@ ruleTester.run('no-identical-tests', rule, {
152194
invalid: []
153195
});
154196
`,
155-
errors: [ERROR],
197+
errors: [ERROR_STRING_TEST],
156198
},
157199
],
158200
});

0 commit comments

Comments
 (0)