-
-
Couldn't load subscription status.
- Fork 31
feat(unique-test-case-names): add rule to enforce unique test case name #561
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
aladdin-add
merged 1 commit into
eslint-community:main
from
michaelfaith:feat/unique-test-case-names
Oct 25, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Enforce that all test cases with names have unique names (`eslint-plugin/unique-test-case-names`) | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| This rule enforces that any test cases that have names defined, have unique names within their `valid` and `invalid` arrays. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule aims to ensure test suites are producing logs in a form that make it easy to identify failing tests, when they occur. | ||
| For thoroughly tested rules, it's not uncommon for test cases to have names defined so that they're easily distinguishable in the test log output. | ||
| Requiring that, within each `valid` and `invalid` group, any test cases with names have unique names, it ensures the test logs are unambiguous. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| new RuleTester().run('foo', bar, { | ||
| valid: [ | ||
| { | ||
| code: 'nin', | ||
| name: 'test case 1', | ||
| }, | ||
| { | ||
| code: 'smz', | ||
| name: 'test case 1', | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: 'foo', | ||
| errors: ['Error'], | ||
| name: 'test case 2', | ||
| }, | ||
| { | ||
| code: 'bar', | ||
| errors: ['Error'], | ||
| name: 'test case 2', | ||
| }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| new RuleTester().run('foo', bar, { | ||
| valid: [ | ||
| { | ||
| code: 'nin', | ||
| name: 'test case 1', | ||
| }, | ||
| { | ||
| code: 'smz', | ||
| name: 'test case 2', | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: 'foo', | ||
| errors: ['Error'], | ||
| name: 'test case 1', | ||
| }, | ||
| { | ||
| code: 'bar', | ||
| errors: ['Error'], | ||
| name: 'test case 2', | ||
| }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you aren't concerned with the nature of test logs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import type { Rule } from 'eslint'; | ||
|
|
||
| import { evaluateObjectProperties, getKeyName, getTestInfo } from '../utils.ts'; | ||
| import type { TestInfo } from '../types.ts'; | ||
|
|
||
| const rule: Rule.RuleModule = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'enforce that all test cases with names have unique names', | ||
| category: 'Tests', | ||
| recommended: false, | ||
| url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/unique-test-case-names.md', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| notUnique: | ||
| 'This test case name is not unique. All test cases with names should have unique names.', | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const sourceCode = context.sourceCode; | ||
|
|
||
| /** | ||
| * Validates test cases and reports them if found in violation | ||
| * @param cases A list of test case nodes | ||
| */ | ||
| function validateTestCases(cases: TestInfo['valid']): void { | ||
| // Gather all of the information from each test case | ||
| const namesSeen = new Set<string>(); | ||
| const violatingNodes: NonNullable<TestInfo['valid'][number]>[] = []; | ||
|
|
||
| cases | ||
| .filter((testCase) => !!testCase) | ||
| .forEach((testCase) => { | ||
| if (testCase.type === 'ObjectExpression') { | ||
| for (const property of evaluateObjectProperties( | ||
| testCase, | ||
| sourceCode.scopeManager, | ||
| )) { | ||
| if (property.type === 'Property') { | ||
| const keyName = getKeyName( | ||
| property, | ||
| sourceCode.getScope(testCase), | ||
| ); | ||
| if ( | ||
| keyName === 'name' && | ||
| property.value.type === 'Literal' && | ||
| typeof property.value.value === 'string' | ||
| ) { | ||
| const name = property.value.value; | ||
| if (namesSeen.has(name)) { | ||
| violatingNodes.push(property.value); | ||
| } else { | ||
| namesSeen.add(name); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| for (const node of violatingNodes) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'notUnique', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| Program(ast) { | ||
| getTestInfo(context, ast).forEach((testInfo) => { | ||
| validateTestCases(testInfo.valid); | ||
| validateTestCases(testInfo.invalid); | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default rule; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { RuleTester } from 'eslint'; | ||
|
|
||
| import rule from '../../../lib/rules/unique-test-case-names.ts'; | ||
|
|
||
| /** | ||
| * Returns the code for some valid test cases | ||
| * @param cases The code representation of valid test cases | ||
| * @returns Code representing the test cases | ||
| */ | ||
| function getTestCases(valid: string[], invalid: string[] = []): string { | ||
| return ` | ||
| new RuleTester().run('foo', bar, { | ||
| valid: [ | ||
| ${valid.join(',\n ')}, | ||
| ], | ||
| invalid: [ | ||
| ${invalid.join(',\n ')}, | ||
| ] | ||
| }); | ||
| `; | ||
| } | ||
|
|
||
| const errorBuffer = 3; // Lines before the test cases start | ||
|
|
||
| const error = (line?: number) => ({ | ||
| messageId: 'notUnique', | ||
| ...(typeof line === 'number' ? { line } : {}), | ||
| }); | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { sourceType: 'module' }, | ||
| }); | ||
| ruleTester.run('unique-test-case-names', rule, { | ||
| valid: [ | ||
| { | ||
| code: getTestCases(['"foo"', '"bar"', '"baz"']), | ||
| name: 'only shorthand strings', | ||
| }, | ||
| { | ||
| code: getTestCases(['"foo"', '"foo"', '"foo"']), | ||
| name: 'redundant shorthand strings', | ||
| }, | ||
| { | ||
| code: getTestCases(['"foo"', '"bar"', '{ code: "foo" }']), | ||
| name: 'shorthand strings and object without name', | ||
| }, | ||
| { | ||
| code: getTestCases([ | ||
| '{ code: "foo" }', | ||
| '{ code: "bar", name: "my test" }', | ||
| ]), | ||
| name: 'object without name and with name', | ||
| }, | ||
| { | ||
| code: getTestCases([ | ||
| '{ code: "foo", name: "my test" }', | ||
| '{ code: "bar", name: "my other test" }', | ||
| ]), | ||
| name: 'object with unique names', | ||
| }, | ||
| { | ||
| code: getTestCases(['foo']), | ||
| name: 'non-string, non-object test case (identifier)', | ||
| }, | ||
| { | ||
| code: getTestCases(['foo()']), | ||
| name: 'non-string, non-object test case (function)', | ||
| }, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: getTestCases([ | ||
| '{ code: "foo", name: "my test" }', | ||
| '{ code: "bar", name: "my other test" }', | ||
| '{ code: "baz", name: "my test" }', | ||
| '{ code: "bla", name: "my other test" }', | ||
| ]), | ||
| errors: [error(errorBuffer + 3), error(errorBuffer + 4)], | ||
| name: 'object with non-unique names', | ||
| }, | ||
| ], | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only duplicate occurrences are tracked, but the first occurrence of a duplicate name is not reported. When a duplicate is found, both the original and duplicate nodes should be reported. Consider tracking nodes associated with each name to report all duplicates, including the first occurrence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was intentional. I was treating the first occurrence as ok, but report every duplicate after. But maybe all should be reported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a pattern for that kind of thing in other rules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's fine - the same as https://github.com/eslint-community/eslint-plugin-eslint-plugin/blob/main/lib/rules/no-identical-tests.ts