Repository: skovhus/jest-codemods
Pull request #620 (2024-09-15 08:14:10)

fix(jasmine): .and.stub() usage is correctly replaced

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/620?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 95.21%. 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 [(`4a829df`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/4a829df3bf287a9bd78a2a3e46b496ce507df4d4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).
> Report is 78 commits behind head on main.

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


```diff
@@            Coverage Diff             @@
##             main     #620      +/-   ##
==========================================
+ Coverage   92.38%   95.21%   +2.82%     
==========================================
  Files          26       27       +1     
  Lines        1944     2089     +145     
  Branches      405      435      +30     
==========================================
+ Hits         1796     1989     +193     
+ Misses        102       92      -10     
+ Partials       46        8      -38     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/620?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 69888a4..5692704 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -176,13 +176,10 @@ export default function jasmineGlobals(fileInfo, api, options) {
           break
         }
         // This is transformed by the *.and.*() expression handling below
+        case 'rejectWith':
         case 'resolveTo': {
           break
         }
-        // This is transformed by the *.and.*() expression handling below
-        case 'rejectWith': {
-          break
-        }
         default: {
           logWarning(
             `Unsupported Jasmine functionality "jasmine.createSpy().and.${spyType}".`,
@@ -362,6 +359,17 @@ export default function jasmineGlobals(fileInfo, api, options) {
           ]
           break
         }
+
+        case 'stub': {
+          if (path.parentPath.node.type === 'MemberExpression') {
+            j(path).replaceWith(path.node.callee.object.object)
+          } else {
+            path.node.callee.object = path.node.callee.object.object
+            path.node.callee.property.name = 'mockImplementation'
+            path.node.arguments = [emptyArrowFn]
+          }
+          break
+        }
       }
     })
 


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 1da75bb..bdd39b1 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -81,6 +81,8 @@ test('spyOn', () => {
     jest.spyOn(stuff).and.rejectWith('oh no');
     const fetchSpy = spyOn(window, 'fetch').and.resolveTo({json: {}});
     existingSpy.and.callThrough();
+    spyOn(something, 'foo').and.stub().and.returnValue(42);
+    mySpy.and.stub();
     `,
     `
     jest.spyOn().mockReturnValue();
@@ -97,6 +99,8 @@ test('spyOn', () => {
     jest.spyOn(stuff).mockRejectedValue('oh no');
     const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}});
     existingSpy.mockRestore();
+    jest.spyOn(something, 'foo').mockReturnValue(42);
+    mySpy.mockImplementation(() => {});
     `
   )
 })