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

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

Merged
merged 5 commits into from
Jan 2, 2024
Merged

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

merged 5 commits into from
Jan 2, 2024

Conversation

danbeam
Copy link
Contributor

@danbeam danbeam commented Mar 25, 2023

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
Copy link
Owner

skovhus commented Mar 25, 2023

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
Copy link
Contributor Author

danbeam commented Nov 15, 2023

@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
Copy link
Owner

skovhus commented Nov 15, 2023

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

@danbeam danbeam changed the title Codemod for auto-creating @jest/globals imports Codemod for auto-creating @jest/globals imports Nov 21, 2023
@danbeam
Copy link
Contributor Author

danbeam commented Nov 21, 2023

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.)

package.json Show resolved Hide resolved
@danbeam danbeam requested a review from skovhus November 21, 2023 22:55
@danbeam
Copy link
Contributor Author

danbeam commented Nov 30, 2023

@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
Copy link
Contributor Author

danbeam commented Dec 12, 2023

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.)

Copy link

codecov bot commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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
@@            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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@skovhus skovhus merged commit 86a209e into skovhus:main Jan 2, 2024
3 checks passed
@skovhus
Copy link
Owner

skovhus commented Jan 2, 2024

Thanks for adding support for this!

@danbeam danbeam deleted the jest-globals branch January 2, 2024 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants