Skip to content

Commit 25ade00

Browse files
committed
Update: no-identical-tests despite of properties order.
1 parent 93bd142 commit 25ade00

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lib/rules/no-identical-tests.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,46 @@ module.exports = {
2929
const message = 'This test case is identical to another case.';
3030
const sourceCode = context.getSourceCode();
3131

32+
// ----------------------------------------------------------------------
33+
// Helpers
34+
// ----------------------------------------------------------------------
35+
/**
36+
*compare two test cases depite of properties order.
37+
*@returns {boolean} if eq, return true, else return false.
38+
*/
39+
function eq (ta, tb) {
40+
const pa = ta.properties || [];
41+
const pb = tb.properties || [];
42+
43+
// if properties length not eq; return false;
44+
if (pa.length !== pb.length) {
45+
return false;
46+
}
47+
48+
// convert array to object
49+
const paObj = pa.reduce((result, item) => {
50+
const code = sourceCode.getText(item);
51+
result[code] = true;
52+
return result;
53+
}, {});
54+
55+
for (let i = 0; i < pb.length; i++) {
56+
const code = sourceCode.getText(pb[i]);
57+
if (!(code in paObj)) {
58+
return false;
59+
}
60+
}
61+
return true;
62+
}
63+
3264
return {
3365
Program (ast) {
3466
utils.getTestInfo(context, ast).forEach(testRun => {
3567
[testRun.valid, testRun.invalid].forEach(tests => {
36-
const cache = Object.create(null);
68+
const cache = [];
3769
// to avoid tests being null
3870
(tests || []).forEach(test => {
39-
const testCode = sourceCode.getText(test);
40-
if (cache[testCode]) {
71+
if (cache.some(item => eq(item, test))) {
4172
context.report({
4273
node: test,
4374
message,
@@ -50,7 +81,7 @@ module.exports = {
5081
},
5182
});
5283
} else {
53-
cache[testCode] = true;
84+
cache.push(test);
5485
}
5586
});
5687
});

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

+20
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,25 @@ ruleTester.run('no-identical-tests', rule, {
106106
});
107107
`,
108108
},
109+
{
110+
code: `
111+
new RuleTester().run('foo', bar, {
112+
valid: [
113+
{ code: 'foo', options: ['bar'] },
114+
{ options: ['bar'], code: 'foo' },
115+
],
116+
invalid: []
117+
});
118+
`,
119+
errors: [ERROR],
120+
output: `
121+
new RuleTester().run('foo', bar, {
122+
valid: [
123+
{ code: 'foo', options: ['bar'] },
124+
],
125+
invalid: []
126+
});
127+
`,
128+
},
109129
],
110130
});

0 commit comments

Comments
 (0)