-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
test: create utility function to infer rule types #176
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
93db673
to
65a8118
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #176 +/- ##
=======================================
Coverage 96.09% 96.09%
=======================================
Files 101 101
Lines 4688 4692 +4
Branches 1613 1613
=======================================
+ Hits 4505 4509 +4
Misses 177 177
Partials 6 6 ☔ View full report in Codecov by Sentry. |
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.
After what discussed in typescript-eslint/typescript-eslint#10321 I would consider to provide plain objects to the RuleTester#run
method:
test
helper function is used to provide a default set of languagesOptions
to each test case but I think that it can be done once when creating a RuleTester
instance:
test/utils
could export aTEST_RULE_DEFAULT_LANGUAGE_OPTIONS
or agetTestRuleDefaultLanguageOptions
function that returns the very same object, which then is passed when creatingRuleTester
instance (if required)test
function will be deprecatedcreateRuleTestCaseFunction
can be removed since the plain objects provided toRuleTester#run
are already type-checked.
What do you think?
There are many variations of
|
Ok, I'll update the two rules I already edited in this PR and then if you like the result we can move on! |
b49cfe5
to
b8d14f3
Compare
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.
LGTM!
During #174 and #175 I noticed that in test files rule
options
types are not inferred insidetest
functions.To avoid this problem I added
createRuleTestCaseFunction
that returns a function that can be used to generate both valid and invalid cases.This function accept only 1 type parameter which is used to infer rule
Options
andMessageIds
which are not exported from the rule source file.Pros
The immediate advantage is that now types are properly inferred from the rule declaration and a type error is reported if the rule
Options
type doesn't match them:Intellisense on rule options screenshot
Additionally also rule
MessageIds
type can be inferred and provided as autocomplete suggestions:Intellisense on rule message ids screenshot
Cons
The only problem is that Invalid test case
errors
property now use themessageId
anddata
structure rather than simple strings: you can see the impact of these changes on the two rules test files changed in this PR.I'm willing to convert all test to this pattern but I understand that this will also increase maintenance efforts while pulling changes from the upstream repository, so no problem if you don't want to proceed with this change 😉.