-
Notifications
You must be signed in to change notification settings - Fork 144
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
Move rules settings to ESLint shared config #198
Comments
I think it pretty much sums up. |
It sounds like a good idea. Also those two utils mentioned should have a nice bunch of unit tests. Our utils don't have tests so it's a good opportunity to start with some. |
I'm gonna start working on it as I finished the rule I was implementing. |
let me know if you need help, I just created a PR and I'm open to taking more tasks; otherwise, I'll grab v3 issues 😅 |
I'm open to help too if you need something 😊 |
@gndelia @renatoagds thanks guys! I think I need to work on changing the config by myself. Then, I'll update rules currently using the renderFunction option to use the new mechanism. So at that point, I think you can help me applying the same mechanism to the remaining rules. I'll let you know when I achieve stuff needed for it. |
Hey guys. Sorry I haven't been active with this one, but I realized about something couple of days ago which I think implies more work than expected related to point 2).
There are more things affected here. Basically, everything could be imported from a custom module! That's actually what Testing Library docs suggests: reexporting everything from this custom module. I didn't realize about it, so technically it would be possible to import other stuff like // other testing library methods reexported from custom module
import { screen, fireEvent } from 'test-utils';
// ...
const button = screen.getByRole('button'); // this screen should be included to be reported too
fireEvent.click(button); // this fireEvent should be included to be reported too So this led me update point 2) to instead of:
we would need to repurpose // the user has setup 'test-utils' as custom testing library module in ESLint shared config
import { screen } from 'test-utils';
import { fireEvent } from '@testing-library/react;
import { render } from '@somewhere/else';
// ...
hasTestingLibraryUtilImport(node, 'screen') // true
hasTestingLibraryUtilImport(node, 'fireEvent') // true
hasTestingLibraryUtilImport(node, 'render') // false I thought this would be enough, but what if the import is renamed? So I think the final approach for point 2) should be a // the user has setup 'test-utils' as custom testing library module in ESLint shared config
import { screen as testingScreen } from 'test-utils';
import { fireEvent } from '@testing-library/react;
import { render } from '@somewhere/else';
// ...
getTestingLibraryUtilImport(node, 'screen') // 'testingScreen'
getTestingLibraryUtilImport(node, 'fireEvent') // 'fireEvent'
getTestingLibraryUtilImport(node, 'render') // null So
Thoughts? |
sorry for the delay in the response! It has been a couple of rough days 😅 I am not sure if alias are a problem in the import - when analyzing the AST you can still match them by the original name and then pick up the alias. From there you'd use that. I think some rules solve it in that way I agree on all of what you say; my only doubt is about Other than that, I agree with the proposal |
Well, maybe we can add a 3rd optional arg to getTestingLibraryUtilImport(node, 'screen') // 'testingScreen'
getTestingLibraryUtilImport(node, 'fireEvent') // 'fireEvent'
getTestingLibraryUtilImport(node, 'userEvent', ['@testing-library/user-event']) // 'userEvent'
getTestingLibraryUtilImport(node, 'userEvent') // null If we go for this option, I'd move this to an object arg to avoid having 3 args. |
@gndelia @timdeschryver @thomlom I'm finding more issues/questions while developing this. I've created a simple draft with couple of functions and their expected usage, so you can see what are my intentions and the problems I found: #217 I'm gonna comment my code there. |
I love this comment from @CreativeTechGuy: #222 (comment) I think this is the right way of checking Testing Library stuff on v4: rather than trying to scope rules to directly imported Testing Library utils by default, the plugin should be really aggressive and report everything matching Testing Library names by default, without caring about where they are imported from. Then, the user can config from which custom module they want to check the utils, or which are the custom queries to be included. It doesn't change that much the implementation of those utils I still have pending. I hope I can check it this week and make some progress. |
Sorry for the very long delay. 😅 Other than that, it would mean just changing the "Default behaviour" from what we've defined as the approach to take; is that correct? |
Exactly, it would mean just changing the default behavior from "report just Testing Library things" to "report everything matching Testing Library patterns". Then when user sets custom renders, queries or something else, we would switch to "report just Testing Library things + custom things". |
So as mechanism to make Testing Library rules seems close in #237 I've done an analysis over shared config we need and how they affect to all rules to be refactored. You can find info about this analysis in this notion doc I wrote down I'll put this on corresponding GitHub issues or project, but first I need to address few of the things commented in that notion doc notes. After that, we can start splitting the job if someone could help 😅 . I can finish this myself otherwise, but I want to share with all you at least for some sanitization checks! |
This one's done! 👊 Congrats to the team! 🥳🎉 |
Finally! I'll add more details about next steps on v4 issue. |
As discussed in different issues and PRs, we need to find a better approach for giving the user the possibility to define which custom render methods they use for Testing Library, or which custom modules they are importing render method.
This change will imply some breaking changes, so v4 would be the perfect opportunity to address this.
The idea is to move specific rules options duplicated across the plugin to ESLint shared settings.
For that reason, this seems the proper approach for avoid duplicating rules configs.
What things do we need to setup there?
As pre-requisite we need to find a way to hook enhanced rules listener with detection helpers for all rules implementations. This has two purposes: extract the common checks (e.g. is imported from Testing Library? is a valid filename?) into a shared create rule function which already prevent rules to be executed if common checks are not met (this allows rules to focus on particular checks for the rule itself without worrying about global checks), and receive a set of detection helpers to detect Testing Library utils like "isQuery" or "isRender" (this allows rules to detect Testing Library patterns easier, reducing duplicated code, complexity and errors).
New shared setting to setup a custom module where Testing Libraries can be imported from other than the official package itself. There is an example here in Testing Library docs. + "aggressive reporting" by default when no custom module set + add corresponding detection helper
New shared setting to setup the name pattern of the files desired to be analyzed (by default with same config from jest) + add corresponding detection helperTHIS WAS FINALLY DISCARDEDrenderFunctions
option from existing rules to shared setting, so users can config there any custom render function they want to be considered consider by eslint-plugin-testing-library to be reported + add corresponding detection helper:(get|query|find)(All)?By
are related to Testing Library itself, doesn't matter if they are built-in ones (e.g.getByText
) or custom ones (e.g.getByIcon
) + remove current rules options for customizing extra queries + add corresponding detection helperMotivation
render
if called other than that so better to set it up globally (applies to 3)render
and 2) strict the scope of analyzed files to those matching their desired pattern (applies to 2, 3)getByIcon
) but they forgot to set it up. Instead, we check if they follow TL query naming pattern and report them as any built-in query. There is nothing to configure here, for now at least. (applies to 4)Extra things
Tasks
New reporting + settings requirements
Setting for filename pattern + detection helper (requirement 2): Move rules settings to ESLint shared config: part 3 - check reported file name #244, refactor: use micromatch for matching files to be reported #296removed in refactor: remove mechanism to match files to be reported #297Rules pending to be refactored
The text was updated successfully, but these errors were encountered: