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

feat: ability to test rule errors and invalid schemas #16823

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

bmish
Copy link
Sponsor Member

@bmish bmish commented Jan 25, 2023

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
[X] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Implements this RFC:

Fixes #13434.

TODO:

  • Wait for RFC approval
  • Copy changes to flat-rule-tester.js
  • Add rule tester tests
  • More documentation
  • Add tests for fatal error when empty schema that's invalid, present schema that has one validation error, and present schema that has multiple validation errors (fix!: Update ajv options to align with aims for behaviour #17356)

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

@eslint-github-bot eslint-github-bot bot added triage An ESLint team member will look at this issue soon feature This change adds a new feature to ESLint labels Jan 25, 2023
@netlify
Copy link

netlify bot commented Jan 25, 2023

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 2b39ad3
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/64bf1aa78cd5c20008cb3540

@github-actions
Copy link

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Feb 12, 2023
@Rec0iL99
Copy link
Member

Rec0iL99 commented Feb 13, 2023

WIP. The implementation proposal was approved recently. Don't close

@@ -1340,6 +1340,7 @@ class Linter {
providedOptions.physicalFilename
);
} catch (err) {
err.messageForTest = err.message; // Original exception message without extra helper text for asserting in fatal test cases.
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I'm not sure how to hit this codepath in the tests. This is in _verifyWithoutProcessors().

Copy link
Member

Choose a reason for hiding this comment

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

I would assume creating a rule that throws an error would hit this?

@bmish bmish marked this pull request as ready for review May 22, 2023 02:50
@bmish bmish requested a review from a team as a code owner May 22, 2023 02:50
@bmish
Copy link
Sponsor Member Author

bmish commented May 22, 2023

This is ready for review.

@fasttime fasttime added accepted There is consensus among the team that this change meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jun 16, 2023
@fasttime
Copy link
Member

Marking as accepted, as this implements a merged RFC.

nzakas
nzakas previously approved these changes Jun 16, 2023
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Seems pretty straightforward. I just left a couple of cleanup suggestions but otherwise, nice work!

Would like @mdjermanovic to review before merging.

docs/src/integrate/nodejs-api.md Outdated Show resolved Hide resolved
@@ -1340,6 +1340,7 @@ class Linter {
providedOptions.physicalFilename
);
} catch (err) {
err.messageForTest = err.message; // Original exception message without extra helper text for asserting in fatal test cases.
Copy link
Member

Choose a reason for hiding this comment

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

I would assume creating a rule that throws an error would hit this?

lib/shared/schema-validation-error.js Outdated Show resolved Hide resolved
@mdjermanovic mdjermanovic added the core Relates to ESLint's core APIs and features label Jun 19, 2023
@@ -836,6 +859,8 @@ In addition to the properties above, invalid test cases can also have the follow
If a string is provided as an error instead of an object, the string is used to assert the `message` of the error.
* `output` (string, required if the rule fixes code): Asserts the output that will be produced when using this rule for a single pass of autofixing (e.g. with the `--fix` command line flag). If this is `null`, asserts that none of the reported problems suggest autofixes.

Fatal test cases (which are optional) are the same as invalid test cases, except that `code` is optional (it may be irrelevant when testing rule options), and there's an `error` object instead of an `errors` array. The `error` object should include one or both of the `message` of the error and the error (exception) class `name`. Fatal test cases can be used to test custom errors thrown by the rule, or invalid rule options (in which case the error `name` will be `SchemaValidationError`, and the `message` will be come from JSON Schema -- note that strings from JSON Schema are subject to change with future upgrades).
Copy link
Member

Choose a reason for hiding this comment

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

"are the same as invalid test cases" would be confusing because fatal and invalid test cases do not share any specific properties (invalid test cases have errors and output; fatal test cases have error).

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Reworded this paragraph to be more clear.

@@ -130,6 +153,7 @@ const RuleTesterParameters = [
"code",
"filename",
"options",
"error",
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to ensure that valid and invalid test cases don't have an error property and that fatal test cases don't have errors or output.

(the same for flat-rule-tester)

@@ -688,6 +746,22 @@ class RuleTester {
try {
SourceCode.prototype.getComments = getCommentsDeprecation;
messages = linter.verify(code, config, filename);
Copy link
Member

Choose a reason for hiding this comment

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

If code wasn't specified in the test case, this will fail with TypeError: Cannot read properties of undefined (reading 'text').

(the same for flat-rule-tester)

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I think I've already tested this scenario with test cases prefixed with:

// omit `code`

Isn't that the scenario you're talking about?

lib/rule-tester/rule-tester.js Outdated Show resolved Hide resolved
lib/rule-tester/rule-tester.js Outdated Show resolved Hide resolved
@nzakas
Copy link
Member

nzakas commented Jul 14, 2023

@bmish can you circle back around to finish this up based on @mdjermanovic's feedback?

bmish and others added 5 commits July 23, 2023 14:09
* main: (101 commits)
  docs: Migrating `eslint-env` configuration comments (eslint#17390)
  Merge pull request from GHSA-qwh7-v8hg-w8rh
  feat: adds option for allowing empty object patterns as parameter (eslint#17365)
  fix: FlatESLint#getRulesMetaForResults shouldn't throw on unknown rules (eslint#17393)
  docs: fix Ignoring Files section in config migration guide (eslint#17392)
  docs: Update README
  feat: Improve config error messages (eslint#17385)
  fix: Update no-loop-func to not overlap with no-undef (eslint#17358)
  docs: Update README
  docs: Update README
  8.45.0
  Build: changelog update for 8.45.0
  chore: package.json update for @eslint/js release
  docs: add playground links to correct and incorrect code blocks (eslint#17306)
  docs: Expand rule option schema docs (eslint#17198)
  docs: Config Migration Guide (eslint#17230)
  docs: Update README
  fix: Fix suggestion message in `no-useless-escape` (eslint#17339)
  docs: Update README
  chore: update eslint-config-eslint exports (eslint#17336)
  ...
@nzakas
Copy link
Member

nzakas commented Jul 28, 2023

@mdjermanovic this one is ready for you to look over again

@bmish bmish marked this pull request as draft August 1, 2023 09:42
@@ -2232,6 +2232,268 @@ describe("RuleTester", () => {
});
});

describe("fatal test cases", () => {
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Moved to draft since it looks like I need to add more test cases to ensure all these scenarios work correctly:

  • empty schema, validation error when options are passed
  • present schema that has one validation error
  • present schema that has multiple validation errors

@fasttime
Copy link
Member

In #17475 we have changed the logic in RuleTester and FlatRuleTester to avoid creating empty suites for valid or invalid tests. In other words, describe will be only called if there are any its to call inside of it.

I was thinking that the same change should be applied to fatal tests: describe("fatal", ... should be only called if any fatal tests were specified.

@nzakas
Copy link
Member

nzakas commented Jan 17, 2024

@bmish what's the status on this? Looks like it hasn't been updated in a while. Are you looking to get this into v9?

@bmish
Copy link
Sponsor Member Author

bmish commented Jan 17, 2024

@nzakas this isn't a breaking change so I don't think it needs to be included in the initial version of ESLint v9. Will aim to get it into a later version.

@nzakas
Copy link
Member

nzakas commented Jan 17, 2024

Sounds good. I'll take it off my radar for now.

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 core Relates to ESLint's core APIs and features feature This change adds a new feature to ESLint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to test rule schemas
5 participants