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

[RuleTester] Test case validation #13917

Closed
DMartens opened this issue Dec 10, 2020 · 6 comments · Fixed by #15425
Closed

[RuleTester] Test case validation #13917

DMartens opened this issue Dec 10, 2020 · 6 comments · Fixed by #15425
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features
Projects

Comments

@DMartens
Copy link
Contributor

The version of ESLint you are using.
Node version: v15.3.0
npm version: v6.14.2
Local ESLint version: v7.15.0 (Currently used)
Global ESLint version: Not found

The problem you want to solve.
The rule tester throws a type error and does not run any test case if the test case property is not a string:

import { RuleTester } from 'eslint';
const ruleTester = new RuleTester();
ruleTester.run("rule-id", rule, {
	valid: [{
		code: [
			'...',
		] // <-- missing .join('\n')
	}],
	invalid: [],
});

which results in an ugly error:

text.replace is not a function 
  at ​​​sanitize​​​ ​./node_modules/eslint/lib/rule-tester/rule-tester.js:207

Relevant code is here.
I categorized this as a change as there is only little test case validation but this issue may also be seen as a bug report.

Your take on the correct solution to problem.
The rule tester only fails the impacted test case with an instruction on how to correct the error.
This may be expanded to other properties than the code property.
Currently the rule tester only checks the structure of the scenarios (eg. valid and invalid are present) and for test cases that options are an array and that parser is an absolute path.

Are you willing to submit a pull request to implement this change?
Yes

@DMartens DMartens added core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint triage An ESLint team member will look at this issue soon labels Dec 10, 2020
@anikethsaha
Copy link
Member

Thanks for the issue

so your proposal is to allow objects, arrays too along with a string for code ?
if this is so, IMO, I wouldn't prefer that as it seems just an extra operation for the Eslint to handle with little usability. It's always better to handle this by the developer itself.

Let me know if I am getting this wrong.

@anikethsaha anikethsaha added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Dec 10, 2020
@DMartens
Copy link
Contributor Author

No I don't propose allowing other types for the code property.
The described problem happens if the code property is not present or is not a string.
The problem is that because the type error is thrown, no test case is executed and you don't know why.
Especially if you have many test cases, it is hard to track down why the error happened.
My proposal is simply to validate the test case properties (in this case the code property) that they have the appropriate type. This would happen inside the test case, so the developer knows which test case has the error.

@anikethsaha
Copy link
Member

Okay, so to add a check for code and throwing appropriate error right?

Make sense to me.

@mdjermanovic mdjermanovic self-assigned this Dec 10, 2020
@mdjermanovic
Copy link
Member

Makes sense to me, it would be useful to provide more helpful info. I'll champion this.

Currently the rule tester only checks the structure of the scenarios (eg. valid and invalid are present) and for test cases that options are an array and that parser is an absolute path.

Some properties are passed through as ESLint config properties and validated as such. For example, try to specify globals: [] or env: [].

These are all RuleTester-specific properties:

const RuleTesterParameters = [
"code",
"filename",
"options",
"errors",
"output"
];

options are validated in runRuleForItem, errors is validated in testInvalidTemplate.

Aside from code, we can consider explicit validations for the remaining two properties, but I think we should limit the scope to only those that certainly cause throwing or failing the test if the type is invalid. Other changes would probably need a RFC.

@nzakas nzakas added this to Needs Triage in Triage via automation Feb 18, 2021
@nzakas nzakas moved this from Needs Triage to Waiting for RFC in Triage Feb 18, 2021
@nzakas nzakas moved this from Waiting for RFC to Evaluating in Triage Feb 18, 2021
@bmish
Copy link
Sponsor Member

bmish commented Dec 14, 2021

I have opened up a fix for this in #15425.

Note that separately, I also have an RFC open for some additional test validations that are breaking changes: eslint/rfcs#84. Please comment on the RFC if there are additional breaking changes you would like to suggest.

@bmish bmish self-assigned this Dec 14, 2021
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 14, 2021
@mdjermanovic
Copy link
Member

Marked as accepted since this is more of a bug than an enhancement.

@mdjermanovic mdjermanovic removed the enhancement This change enhances an existing feature of ESLint label Dec 14, 2021
@mdjermanovic mdjermanovic moved this from Evaluating to Pull Request Opened in Triage Dec 14, 2021
Triage automation moved this from Pull Request Opened to Complete Dec 14, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 13, 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 Jun 13, 2022
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 archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly core Relates to ESLint's core APIs and features
Projects
Archived in project
Triage
Complete
Development

Successfully merging a pull request may close this issue.

4 participants