Repository: skovhus/jest-codemods
Pull request #580 (2024-05-22 19:04:01)

fix(`jasmine-globals`): fix transform for existing `spyOn`
Closes https://github.com/skovhus/jest-codemods/issues/579

Better handling of usage of `existingSpy.and.callThrough()`. This follows the same pattern as the legacy `andCallThrough` logic [here](https://github.com/puglyfe/jest-codemods/blob/fix-existing-spy-callthrough/src/transformers/jasmine-globals.ts#L392-L405).

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/580?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.62%. 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 [(`c3834dd`)](https://app.codecov.io/gh/skovhus/jest-codemods/pull/580?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 51 commits behind head on main.


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


```diff
@@            Coverage Diff             @@
##             main     #580      +/-   ##
==========================================
+ Coverage   92.38%   92.62%   +0.23%     
==========================================
  Files          26       27       +1     
  Lines        1944     2007      +63     
  Branches      405      415      +10     
==========================================
+ Hits         1796     1859      +63     
  Misses        102      102              
  Partials       46       46              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/580?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/jasmine-globals.ts b/src/transformers/jasmine-globals.ts
index 6947193..d3ec1af 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -161,10 +161,17 @@ export default function jasmineGlobals(fileInfo, api, options) {
         // if it's `*.and.callThrough()` we should remove
         // `and.callThrough()` because jest calls through by default
         case 'callThrough': {
-          const { callee } = path.node.callee.object.object
-          const arg = path.node.callee.object.object.arguments
-          path.node.callee = callee
-          path.node.arguments = arg
+          // if this comes from an `Identifier` (e.g. `existingSpy.and.callThrough()`),
+          // we assume the intent is to restore an existing spy
+          // to its original implementation using `*.mockRestore()`
+          if (path.node.callee.object.object.type === 'Identifier') {
+            path.node.callee.object = path.node.callee.object.object
+            path.node.callee.property.name = 'mockRestore'
+          } else {
+            // otherwise, we just remove the `.and.callThrough()`
+            // since this is the default behavior in jest
+            j(path).replaceWith(path.node.callee.object.object)
+          }
           break
         }
         // if it's `*.and.callFake()`, replace with jest's


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index d2da182..03980f0 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -28,6 +28,7 @@ test('spyOn', () => {
     jest.spyOn(stuff).and.resolveTo('lmao');
     jest.spyOn(stuff).and.rejectWith('oh no');
     const fetchSpy = spyOn(window, 'fetch').and.resolveTo({json: {}});
+    existingSpy.and.callThrough();
     `,
     `
     jest.spyOn().mockReturnValue();
@@ -42,6 +43,7 @@ test('spyOn', () => {
     jest.spyOn(stuff).mockResolvedValue('lmao');
     jest.spyOn(stuff).mockRejectedValue('oh no');
     const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}});
+    existingSpy.mockRestore();
     `
   )
 })