-
Notifications
You must be signed in to change notification settings - Fork 236
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
[new-rule] prefer importing from @jest/globals #1101
[new-rule] prefer importing from @jest/globals #1101
Comments
I think you should be able to handle this with the existing tooling and education - e.g. so long as you don't have Having said that the core of the work for this is now already done via the PR you mentioned, so a rule shouldn't be expensive (though I don't know if we could have a autofix to add the import without adding a bunch of complexity). What is missing first is the follow up work I'm going to do: moving from a conditional check to returning information about the reference (which is needed to handle Im planning to merge that PR and then do this follow up work over the next couple of weeks :) (I've had a few things pop up at work and need to do some stuff on my |
Note that you can run jest with |
My main interest was actually in easing migration to not using globals by having an automatically fixable rule. Possibly also enabling gradual migration (where @G-Rath What kind of complexity/issues were you thinking auto-fix might run into? I've written lint rules before but I'm less familiar with writing fixers, and not familiar yet with this repo's codebase. |
@ecraig12345 definitely - overall I think it makes sense for us to have the rule given how small it should be and it will be a lot easier to use than the alternatives you can do today. In terms of complexity of auto-fixing I thought it through as I was replying and realised I don't think it's as initially complex as I thought - we should be able to safely find the top level of the whole file (we're doing that in other rules) and insert the import. We should also be able to handle adding to an existing import, as that information should also already be being captured by way of the scope reference checking (it just needs to be exposed). I think for now the biggest question is if we can actually make this an auto fix or if it has to be a suggestion - the trouble is the risk of conflicting with existing definitions. While we'll already know if a reference exists in the current scope, we won't know if there's another scope that has a reference with the same name that'd then be shadowed, e.g.
Remember the general expectation for auto fixers is that they're not breaking - from what I've seen the community tends to lean on the side of caution even when there's just weird probably-nonsense edge cases like this. I'm not worried about it right now though, as the first thing is to just create the rule (technically the second thing 😅) then we can figure out the finer details :) |
@G-Rath I started working on this and had a few questions. (Also noticed the scope issue that you already fixed, nice!)
|
@ecraig12345 I love your enthusiasm but it sounds like you're we've doubling up as I've been implementing the same thing via #1106 - I think I'm almost at the point of doing the next PR as of this morning, so hoping to have it landed by early next week, at which point implementing this rule should be a pretty quick job. I'm effectively re-writing all of these utils to be a core Landing #1106 should take care of your second point (as it moves Regarding performance: it should be fine - we've never had any complaints on performance and what we're doing is all in memory so should be pretty fast; having said that I do plan to do a second-pass over the codebase (mixed with some light performance testing for peace of mind) once all this has been done as I know we definitely are doing a bunch of the same work over and over which we might be able to optimise with a little restructuring and maybe introduce some caching. I'm hesitant to add it initially though because it makes testing a lot harder vs there not currently being a noticeable problem (javascript is typically really performant with these kind of in-memory calls). re the rule name, what about |
I'm fine with waiting on the work you have in progress. I had noticed the |
Ah I'd forgotten we had it as a recommended - I think it would still make sense to use that rule for this since it's a stylistic choice so we can just put it behind an option. Having said that, I am actually now wondering if we should be creating a new singular rule all together because:
I'll have a think on this for future. |
did this move forward somehow? is there an eslint rule to enforce importing from |
@piotrpalek no we've not yet got a rule for this, though you can enforce it at runtime via config. |
🎉 This issue has been resolved in version 28.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
It would be nice to have a rule (with fixer) enforcing that APIs such as
expect
,describe
,it
,jest
, etc should be imported from@jest/globals
. If there's interest in this rule, I'd probably be willing to implement it.Motivation:
"types": ["jest"]
in a tsconfig@types/jest
which can be outdated, mismatched, and/or pull in extra versions of packages (this is an issue as of writing--jest 28 has released but the types are still on 27 and pull in some outdated deps from 27)Here's an example of code with the rule enabled, and an error it might catch:
(I assume this would be blocked by #556, but looks like there's an approved PR #1094 addressing that.)
The text was updated successfully, but these errors were encountered: