Repository: skovhus/jest-codemods
Pull request #575 (2024-05-10 16:32:29)

fix(`jest-globals-import`): fix false negative in used globals check
Closes https://github.com/skovhus/jest-codemods/issues/574

This adds an extra check to ensure that any used jest globals don't already have a corresponding `ImportSpecifier`.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/575?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 92.60%. Comparing base [(`a94e2b5`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/a94e2b561fed619cbebf0b9c28e0a919c44aa4e3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) to head [(`654e875`)](https://app.codecov.io/gh/skovhus/jest-codemods/pull/575?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).
> Report is 47 commits behind head on main.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #575      +/-   ##
==========================================
+ Coverage   92.38%   92.60%   +0.22%     
==========================================
  Files          26       27       +1     
  Lines        1944     2002      +58     
  Branches      405      415      +10     
==========================================
+ Hits         1796     1854      +58     
  Misses        102      102              
  Partials       46       46              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/575?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).


Patch:
diff --git a/src/transformers/jest-globals-import.ts b/src/transformers/jest-globals-import.ts
index 54f5b07..0a24eff 100644
--- a/src/transformers/jest-globals-import.ts
+++ b/src/transformers/jest-globals-import.ts
@@ -1,4 +1,10 @@
-import type { ImportSpecifier, JSCodeshift } from 'jscodeshift'
+import type {
+  ASTPath,
+  CallExpression,
+  ImportSpecifier,
+  JSCodeshift,
+  MemberExpression,
+} from 'jscodeshift'
 
 import { JEST_GLOBALS } from '../utils/consts'
 import { findImports, removeRequireAndImport } from '../utils/imports'
@@ -12,15 +18,25 @@ const jestGlobalsImport = (
 
   const jestGlobalsUsed = new Set<string>()
 
+  const isImplicitlyInScope = (
+    expression: ASTPath<CallExpression | MemberExpression>,
+    globalName: string
+  ) => {
+    return Boolean(
+      expression.scope.lookup(globalName) &&
+        ast.find(j.ImportSpecifier, { imported: { name: globalName } }).size() === 0
+    )
+  }
+
   JEST_GLOBALS.forEach((globalName) => {
     if (
       ast
         .find(j.CallExpression, { callee: { name: globalName } })
-        .filter((callExpression) => !callExpression.scope.lookup(globalName))
+        .filter((callExpression) => !isImplicitlyInScope(callExpression, globalName))
         .size() > 0 ||
       ast
         .find(j.MemberExpression, { object: { name: globalName } })
-        .filter((memberExpression) => !memberExpression.scope.lookup(globalName))
+        .filter((memberExpression) => !isImplicitlyInScope(memberExpression, globalName))
         .size() > 0
     ) {
       jestGlobalsUsed.add(globalName)


Test patch:
diff --git a/src/transformers/jest-globals-import.test.ts b/src/transformers/jest-globals-import.test.ts
index f352c07..cc49efe 100644
--- a/src/transformers/jest-globals-import.test.ts
+++ b/src/transformers/jest-globals-import.test.ts
@@ -75,7 +75,7 @@ const test = () => { console.log('only a test'); };
     )
   })
 
-  it('removes imports', () => {
+  it('removes unnecessary imports', () => {
     expectTransformation(
       `
 import '@jest/globals';
@@ -101,6 +101,30 @@ const BLAH = 5;
 `.trim(),
       `
 const BLAH = 5;
+`.trim()
+    )
+    expectTransformation(
+      `
+import { expect } from '@jest/globals';
+const BLAH = 5;
+expect(BLAH).toBe(5);
+`.trim(),
+      `
+import { expect } from '@jest/globals';
+const BLAH = 5;
+expect(BLAH).toBe(5);
+`.trim()
+    )
+    expectTransformation(
+      `
+import '@jest/globals';
+const BLAH = 5;
+expect(BLAH).toBe(5);
+`.trim(),
+      `
+import { expect } from '@jest/globals';
+const BLAH = 5;
+expect(BLAH).toBe(5);
 `.trim()
     )
   })