Repository: skovhus/jest-codemods
Pull request #578 (2024-05-14 15:31:25)

feat(`jasmine-globals`): support async `SpyStrategy` methods
Closes https://github.com/skovhus/jest-codemods/issues/577

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/578?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.63%. 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 [(`544bb64`)](https://app.codecov.io/gh/skovhus/jest-codemods/pull/578?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 49 commits behind head on main.


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


```diff
@@            Coverage Diff             @@
##             main     #578      +/-   ##
==========================================
+ Coverage   92.38%   92.63%   +0.24%     
==========================================
  Files          26       27       +1     
  Lines        1944     2010      +66     
  Branches      405      415      +10     
==========================================
+ Hits         1796     1862      +66     
  Misses        102      102              
  Partials       46       46              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/578?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 06f9725..6947193 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -53,6 +53,14 @@ export default function jasmineGlobals(fileInfo, api, options) {
           path.node.arguments = [j.arrowFunctionExpression([], path.node.arguments[0])]
           break
         }
+        // This is transformed by the *.and.*() expression handling below
+        case 'resolveTo': {
+          break
+        }
+        // This is transformed by the *.and.*() expression handling below
+        case 'rejectWith': {
+          break
+        }
         default: {
           logWarning(
             `Unsupported Jasmine functionality "jasmine.createSpy().and.${spyType}".`,
@@ -137,6 +145,8 @@ export default function jasmineGlobals(fileInfo, api, options) {
       // - `existingSpy.and.callFake(..)`
       // - `spyOn().and.returnValue(..)`
       // - `existingSpy.and.returnValue(..)`
+      // - `spyOn().and.resolveTo(..)`
+      // - `existingSpy.and.rejectWith(..)`
       callee: {
         type: 'MemberExpression',
         object: {
@@ -171,6 +181,20 @@ export default function jasmineGlobals(fileInfo, api, options) {
           path.node.callee.property.name = 'mockReturnValue'
           break
         }
+        // `*.and.resolveTo()` is equivalent of jest
+        // `*.mockResolvedValue()`
+        case 'resolveTo': {
+          path.node.callee.object = path.node.callee.object.object
+          path.node.callee.property.name = 'mockResolvedValue'
+          break
+        }
+        // `*.and.rejectWith()` is equivalent of jest
+        // `*.mockRejectedValue()`
+        case 'rejectWith': {
+          path.node.callee.object = path.node.callee.object.object
+          path.node.callee.property.name = 'mockRejectedValue'
+          break
+        }
       }
     })
 


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 1d74c48..d2da182 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -25,6 +25,9 @@ test('spyOn', () => {
     jest.spyOn();
     spyOn(stuff);
     jest.spyOn().mockImplementation();
+    jest.spyOn(stuff).and.resolveTo('lmao');
+    jest.spyOn(stuff).and.rejectWith('oh no');
+    const fetchSpy = spyOn(window, 'fetch').and.resolveTo({json: {}});
     `,
     `
     jest.spyOn().mockReturnValue();
@@ -36,6 +39,9 @@ test('spyOn', () => {
     jest.spyOn();
     jest.spyOn(stuff).mockImplementation(() => {});
     jest.spyOn().mockImplementation();
+    jest.spyOn(stuff).mockResolvedValue('lmao');
+    jest.spyOn(stuff).mockRejectedValue('oh no');
+    const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}});
     `
   )
 })
@@ -49,6 +55,9 @@ test('jasmine.createSpy', () => {
     jasmine.createSpy().and.callFake(arg => arg);
     jasmine.createSpy().and.returnValue('lmao');
     const spy2 = jasmine.createSpy().and.returnValue('lmao');
+    jasmine.createSpy().and.resolveTo('lmao');
+    jasmine.createSpy().and.rejectWith('oh no');
+    const spy3 = jasmine.createSpy().and.resolveTo('lmao');
     `,
     `
     jest.fn();
@@ -57,8 +66,14 @@ test('jasmine.createSpy', () => {
     jest.fn(arg => arg);
     jest.fn(() => 'lmao');
     const spy2 = jest.fn(() => 'lmao');
+    jest.fn().mockResolvedValue('lmao');
+    jest.fn().mockRejectedValue('oh no');
+    const spy3 = jest.fn().mockResolvedValue('lmao');
     `
   )
+
+  // Ensure we haven't missed any console warnings
+  expect(consoleWarnings).toEqual([])
 })
 
 test('not supported jasmine.createSpy().and.*', () => {