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

Change Request: Stricter rule test validation #15104

Closed
1 task done
bmish opened this issue Sep 25, 2021 · 10 comments · Fixed by eslint/rfcs#84, #17955 or #17654
Closed
1 task done

Change Request: Stricter rule test validation #15104

bmish opened this issue Sep 25, 2021 · 10 comments · Fixed by eslint/rfcs#84, #17955 or #17654
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion breaking This change is backwards-incompatible core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint

Comments

@bmish
Copy link
Sponsor Member

bmish commented Sep 25, 2021

ESLint version

8.0.0-rc.0

What problem do you want to solve?

  • Currently, it's easy to forget to assert what suggestions a test case yields, or that a test case yields no assertions. As a result, this can be an area of poor test coverage, leading to bugs around suggestions.
  • It's also inconvenient to have to add suggestions: [] to every single invalid test case for a rule or tests that do not produce suggestions, in order to achieve the desired test coverage.

What do you think is the correct solution?

Invalid test cases that yield suggestions should require a suggestions assertion.

This would be the same as how invalid test cases that produce an autofix require an output assertion, and fail with this error:

AssertionError [ERR_ASSERTION]: The rule fixed the code. Please add 'output' property.

This behavior was added as a breaking change in ESLint v7.

We should add a similar assertion for suggestions.

AssertionError [ERR_ASSERTION]: The rule violation produced a suggestion. Please add a 'suggestions' property to the error object.

Example of invalid test case with suggestions assertion:

new RuleTester().run('example-rule', rule, {
  valid: [],
  invalid: [
    {
      code: `foo();`,
      output: null,
      errors: [
        {
          messageId: 'bar',
          type: 'Property',
          suggestions: [], // or array of suggestions if rule provides them
        },
      ],
    },
  ]
});

Participation

  • I am willing to submit a pull request for this change.

Additional comments

No response

@bmish bmish added enhancement This change enhances an existing feature of ESLint triage An ESLint team member will look at this issue soon core Relates to ESLint's core APIs and features labels Sep 25, 2021
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Sep 25, 2021
@nzakas
Copy link
Member

nzakas commented Sep 28, 2021

This seems reasonable to me, however, it is a breaking change and would need to wait until ESLint v9.0.0, which won’t be until next year. That’s a really long time to wait, so not sure the best path forward here.

@nzakas nzakas moved this from Needs Triage to Feedback Needed in Triage Sep 28, 2021
@nzakas nzakas removed the triage An ESLint team member will look at this issue soon label Sep 28, 2021
@bmish
Copy link
Sponsor Member Author

bmish commented Sep 28, 2021

Would this require an RFC? If so, I can put that together so that it can be lined up for v9.

Also, have we begun tracking breaking changes for v9 yet? Even though it's a year away, I'd like to make sure this gets on the list (assuming it gets accepted).

@nzakas
Copy link
Member

nzakas commented Sep 29, 2021

Yes, we would need an RFC.

and yes, we are tracking features for v9 on the v9 project.

@github-actions
Copy link

Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Nov 28, 2021
@bmish bmish self-assigned this Nov 29, 2021
@bmish bmish removed the Stale label Nov 29, 2021
@bmish
Copy link
Sponsor Member Author

bmish commented Nov 29, 2021

Still planning to do this, assigned myself.

@bmish
Copy link
Sponsor Member Author

bmish commented Dec 1, 2021

I have opened an RFC for this issue, along with some related rule test validation improvements: eslint/rfcs#84

@nzakas nzakas added this to Planned in Public Roadmap via automation Dec 2, 2021
@mdjermanovic mdjermanovic moved this from Planned to RFC Under Review in Public Roadmap Jan 27, 2022
@mdjermanovic mdjermanovic moved this from Feedback Needed to RFC Opened in Triage Jan 27, 2022
@fisker
Copy link
Contributor

fisker commented Feb 25, 2022

👎 for this.

In some rule, I only add one test for the suggestions, maybe because it's simple and good enough, then focus other tests on the auto-fix or something else, eg "message", "report loaction", why I need assert suggestions in all those?

The tester should not force anything, test one thing in one case is good practice!

@nzakas
Copy link
Member

nzakas commented Feb 26, 2022

Please review the RFC and leave your comments there.

Public Roadmap automation moved this from RFC Under Review to Complete May 17, 2022
Triage automation moved this from RFC Opened to Complete May 17, 2022
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Nov 14, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Nov 14, 2022
@bmish
Copy link
Sponsor Member Author

bmish commented Dec 15, 2022

Reopening since the RFC hasn't been implemented yet.

@bmish bmish reopened this Dec 15, 2022
Public Roadmap automation moved this from Complete to Planned Dec 15, 2022
Triage automation moved this from Complete to Evaluating Dec 15, 2022
@bmish bmish moved this from Evaluating to Ready to Implement in Triage Dec 15, 2022
@bmish bmish added this to Need Discussion in v9.0.0 Dec 15, 2022
@bmish bmish removed the archived due to age This issue has been archived; please open a new issue for any further discussion label Dec 15, 2022
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion breaking This change is backwards-incompatible labels Jan 6, 2023
@mdjermanovic mdjermanovic changed the title Change Request: Invalid test cases that yield suggestions should require a suggestions assertion Change Request: Stricter rule test validation Jul 21, 2023
@mdjermanovic
Copy link
Member

Since the related eslint/rfcs#84 includes more changes than just requiring suggestion assertions, I changed the title of this issue so that it tracks all the changes, i.e. the full implementation of eslint/rfcs#84:

We will add additional assertions to the rule test runner to address several potential points of poor test coverage, especially related to rule suggestions.

Assertions added to invalid test case error objects:

  • Must contain suggestions if the test case produces suggestions
  • Must contain the violation message (message or messageId, but not both)

Assertions added to invalid test case suggestion objects:

  • Must contain the suggestion code output
  • Must contain the suggestion message (messageId or desc, but not both)
  • The suggestion code output must differ from the test case code

Assertions added to all test cases:

  • Any autofix output must differ from the test case code
  • Cannot have identical test cases
  • The optional filename property must be a string
  • The optional only property must be a boolean

Public Roadmap automation moved this from Planned to Complete Jan 20, 2024
@mdjermanovic mdjermanovic reopened this Jan 20, 2024
Public Roadmap automation moved this from Complete to Planned Jan 20, 2024
@nzakas nzakas linked a pull request Jan 25, 2024 that will close this issue
10 tasks
Public Roadmap automation moved this from Planned to Complete Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion breaking This change is backwards-incompatible core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint
Projects
Public Roadmap
  
Complete
Archived in project
Status: Done
Triage
Ready to Implement
v9.0.0
Need Discussion
4 participants