Skip to content

Commit 3a6811b

Browse files
committed
New: disallow identical tests
1 parent 58a927a commit 3a6811b

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

lib/rules/no-identical-tests.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @fileoverview disallow identical tests
3+
* @author 薛定谔的猫<[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
const utils = require('../utils');
9+
10+
// ------------------------------------------------------------------------------
11+
// Rule Definition
12+
// ------------------------------------------------------------------------------
13+
14+
module.exports = {
15+
meta: {
16+
docs: {
17+
description: 'disallow identical tests',
18+
category: 'Tests',
19+
recommended: true,
20+
},
21+
fixable: null, // or "code" or "whitespace"
22+
schema: [],
23+
},
24+
25+
create (context) {
26+
// ----------------------------------------------------------------------
27+
// Public
28+
// ----------------------------------------------------------------------
29+
const message = 'This test case should not be identical to someone else.';
30+
const sourceCode = context.getSourceCode();
31+
32+
return {
33+
Program (ast) {
34+
utils.getTestInfo(context, ast).forEach(testRun => {
35+
[testRun.valid, testRun.invalid].forEach(tests => {
36+
const cache = Object.create(null);
37+
(tests || []).forEach(test => {
38+
const testCode = sourceCode.getText(test);
39+
if (cache[testCode]) {
40+
context.report({
41+
node: test,
42+
message,
43+
});
44+
} else {
45+
cache[testCode] = true;
46+
}
47+
});
48+
});
49+
});
50+
},
51+
};
52+
},
53+
};

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

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @fileoverview disallow identical tests
3+
* @author 薛定谔的猫<[email protected]>
4+
*/
5+
6+
'use strict';
7+
8+
// ------------------------------------------------------------------------------
9+
// Requirements
10+
// ------------------------------------------------------------------------------
11+
12+
const rule = require('../../../lib/rules/no-identical-tests');
13+
const RuleTester = require('eslint').RuleTester;
14+
15+
const ERROR = { message: 'This test case should not be identical to someone else.' };
16+
17+
// ------------------------------------------------------------------------------
18+
// Tests
19+
// ------------------------------------------------------------------------------
20+
21+
const ruleTester = new RuleTester();
22+
ruleTester.run('no-identical-tests', rule, {
23+
valid: [
24+
`
25+
new RuleTester().run('foo', bar, {
26+
valid: [
27+
{ code: 'foo' },
28+
{ code: 'bar' }
29+
],
30+
invalid: []
31+
});
32+
`,
33+
],
34+
35+
invalid: [
36+
{
37+
code: `
38+
new RuleTester().run('foo', bar, {
39+
valid: [
40+
{ code: 'foo' },
41+
{ code: 'foo' }
42+
],
43+
invalid: []
44+
});
45+
`,
46+
errors: [ERROR],
47+
},
48+
{
49+
code: `
50+
new RuleTester().run('foo', bar, {
51+
valid: [
52+
{ code: 'foo' },
53+
{ code: 'foo' },
54+
],
55+
invalid: [
56+
{ code: 'foo', errors: ['bar'] },
57+
{ code: 'foo', errors: ['bar'] },
58+
]
59+
});
60+
`,
61+
errors: [ERROR, ERROR],
62+
},
63+
],
64+
});

0 commit comments

Comments
 (0)