Skip to content
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

fix: rule tester ignore irrelevant test case properties #18235

Conversation

DMartens
Copy link
Contributor

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[x] Other, please explain: Rule Tester

What changes did you make? (Give an overview)

Fixes #18173.
Ignore the properties name, errors and output as they are irrelevant when checking for duplicate test cases by using the replacer argument of the JSON stringifier.
Unfortunately the custom JSON stringifier does not support the array form of the JSON.stringify replacer.
Also normalized valid string test cases to the object form, as otherwise the following would not be detected:

ruleTester.run('Undetected', rule, {
	valid: [
		'same',
		{ code: 'same' },
	],
	invalid: [],
})

Is there anything you'd like reviewers to focus on?

Some additional duplicate test cases of core rules were flagged and I tried to resolve the errors the best I could.
For example some test cases checked only the location and later other properties were tested.
Rather than merging the test cases, I opted to change the identifier names to keep the intent of the test cases.

@DMartens DMartens requested a review from a team as a code owner March 27, 2024 20:54
@eslint-github-bot
Copy link

Hi @DMartens!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.
  • The length of the commit message must be less than or equal to 72

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@github-actions github-actions bot added the core Relates to ESLint's core APIs and features label Mar 27, 2024
Copy link

netlify bot commented Mar 27, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 9a92dd1
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/660c7429efe8e80008304903

@DMartens DMartens changed the title fix(rule-tester): ignore irrelevant test case properties for the test case duplication check fix: rule tester ignore irrelevant test case properties for the test case duplication check Mar 27, 2024
@eslint-github-bot
Copy link

Hi @DMartens!, thanks for the Pull Request

The pull request title isn't properly formatted. We ask that you update the pull request title to match this format, as we use it to generate changelogs and automate releases.

  • The length of the commit message must be less than or equal to 72

To Fix: You can fix this problem by clicking 'Edit' next to the pull request title at the top of this page.

Read more about contributing to ESLint here

@DMartens DMartens changed the title fix: rule tester ignore irrelevant test case properties for the test case duplication check fix: rule tester ignore irrelevant test case properties Mar 27, 2024
@eslint-github-bot eslint-github-bot bot added the bug ESLint is working incorrectly label Mar 27, 2024
Copy link
Member

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a question about the tests, otherwise LGTM! 👍

tests/lib/rules/accessor-pairs.js Show resolved Hide resolved
tests/lib/rules/accessor-pairs.js Show resolved Hide resolved
tests/lib/rules/no-useless-backreference.js Show resolved Hide resolved
@aladdin-add aladdin-add added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 28, 2024
Comment on lines 875 to 876
const normalizedItem = typeof item === "string" ? { code: item } : item;
const serializedTestCase = stringify(normalizedItem, { replacer: (key, value) => (!duplicationIgnoredParameters.has(key) ? value : void 0) });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only exclude top-level test case properties. Otherwise, if the rule has options with these names, test cases like the following would be mistakenly detected as duplicates:

{
    code: "var foo;",
    options: [{ name: "bar" }]
},
{
    code: "var foo;",
    options: [{ name: "baz" }]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I resolved this via checking whether the current context object this is the root test case object.

aladdin-add
aladdin-add previously approved these changes Apr 2, 2024
Copy link
Member

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Would like another review before merging.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly contributor pool core Relates to ESLint's core APIs and features
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Change Request: Rule Tester omit specific properties for duplicate test case detection
3 participants