skovhus / jest-codemods

Codemods for migrating to Jest https://github.com/facebook/jest 👾
MIT License
878 stars 81 forks source link

Codemod for auto-creating `@jest/globals` imports #508

Closed danbeam closed 8 months ago

danbeam commented 1 year ago

I recently had to convert a bit of code that used jest globals (e.g. describe(), it(), expect()) to explicitly importing these APIs from @jest/globals.

Before:

it('works', () => {
  expect(true).toBe(true);
});

After:

import { expect, it } from '@jest/globals';

it('works', () => {
  expect(true).toBe(true);
});

While doing that manually I thought: this would make a good codemod!

So I wrote a fairly simple one that actually scrapes the exports from the @jest/globals package itself[1], finds global usage, and adds imports.

[1] I don't use require.resolve() nor actually import it cuz both are enforcedly disallowed, which is a mild bummer, but overall I think it's still kinda slick. If you know a better way, let me know!

skovhus commented 1 year ago

Interesting! Thanks for another contribution! I just read quickly through this, and here is my feedback.

Note that we have similar code for this for all the transformations – in the CLI you can pick what we internal call standaloneMode if this you say no to "Will you be using Jest on Node.js as your test runner?". This is done here: https://github.com/skovhus/jest-codemods/blob/main/src/utils/finale.ts#L25 Not sure how many people use this feature... It seems we might want to use @jest/globals instead of jest-mock and expect as we do right now. Also rather poorly documented here: https://github.com/skovhus/jest-codemods#test-environment-jest-on-nodejs-or-other

We also have a bunch of import helpers defined here, that could be useful for this PR. Maybe they handle more corner cases than you do, or maybe you handle additional corner cases: https://github.com/skovhus/jest-codemods/blob/main/src/utils/imports.ts

Currently all codemods converts from one test runner to another, this one is different. I guess it shouldn't be exposed in the CLI or? But the transformation here could just be exposed directly as these: https://github.com/skovhus/jest-codemods#usage-jscodeshift

danbeam commented 10 months ago

@skovhus sorry for the lag on this; do you want the content of this PR (a @jest/globals codemod)? It wasn't clear from your previous response.

If "yes", I can probably find some time to e.g. update to utils/imports.ts or something along those lines. But I kinda wanted to know whether you actually want the content first 😅 .

If "no": I can close.

Currently all codemods converts from one test runner to another, this one is different. I guess it shouldn't be exposed in the CLI or? But the transformation here could just be exposed directly as these: https://github.com/skovhus/jest-codemods#usage-jscodeshift

I don't have strong feelings here; whatever you prefer.

skovhus commented 10 months ago

This is great. Let us just document it in the README and then I think we are good to go.

danbeam commented 9 months ago

This is great. Let us just document it in the README and then I think we are good to go.

I can add more prose about this codemod, but it seems fairly discretionary about what you'd like to say (e.g. do you want another sentence before the status images? I don't know that this is worth highlighting like that.)

danbeam commented 9 months ago

@skovhus anything else you'd like me to do here?

I haven't quite figured out how to CI checks passing (I'm getting different results locally, I think?).

Note: when I tried updating the lock file, the format was different and I don't think the CI checks passed then either (but I think perhaps it was a different error at least?).

danbeam commented 9 months ago

hey @skovhus 👋

just following up again here: anything I can do to get this done? it's not particularly urgent, I'm just hoping to tie up loose ends by getting this in eventually! (when it's right by you)

let me know if you can! (if you're on holiday or something no sweat.)

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (a94e2b5) 92.38% compared to head (dce4d86) 92.59%. Report is 28 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #508 +/- ## ========================================== + Coverage 92.38% 92.59% +0.20% ========================================== Files 26 27 +1 Lines 1944 1999 +55 Branches 405 414 +9 ========================================== + Hits 1796 1851 +55 Misses 102 102 Partials 46 46 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

skovhus commented 8 months ago

Thanks for adding support for this!