Repository: skovhus/jest-codemods
Pull request #619 (2024-09-14 19:49:23)

feat(jasmine): handle spy.throwError

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

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


```diff
@@            Coverage Diff             @@
##             main     #619      +/-   ##
==========================================
+ Coverage   92.38%   95.19%   +2.81%     
==========================================
  Files          26       27       +1     
  Lines        1944     2082     +138     
  Branches      405      432      +27     
==========================================
+ Hits         1796     1982     +186     
+ 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/619?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 63346e5..69888a4 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -341,6 +341,27 @@ export default function jasmineGlobals(fileInfo, api, options) {
           path.node.callee.property.name = 'mockRejectedValue'
           break
         }
+
+        case 'throwError': {
+          const throwArg = path.node.arguments[0]
+          const isStringLiteral =
+            throwArg.type === 'Literal' && typeof throwArg.value === 'string'
+
+          path.node.callee.object = path.node.callee.object.object
+          path.node.callee.property.name = 'mockImplementation'
+
+          path.node.arguments = [
+            j.arrowFunctionExpression(
+              [],
+              isStringLiteral
+                ? j.blockStatement([
+                    j.throwStatement(j.newExpression(j.identifier('Error'), [throwArg])),
+                  ])
+                : j.blockStatement([j.throwStatement(throwArg)])
+            ),
+          ]
+          break
+        }
       }
     })
 


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 7c6b706..1da75bb 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -126,6 +126,9 @@ test('jasmine.createSpy', () => {
     jasmine.createSpy().and.resolveTo('lmao');
     jasmine.createSpy().and.rejectWith('oh no');
     const spy3 = jasmine.createSpy().and.resolveTo('lmao');
+    spy.and.throwError('my error message');
+    spy.and.throwError(new Error('awesome error'));
+    spy.and.throwError(42);
     `,
     `
     jest.fn();
@@ -137,6 +140,15 @@ test('jasmine.createSpy', () => {
     jest.fn().mockResolvedValue('lmao');
     jest.fn().mockRejectedValue('oh no');
     const spy3 = jest.fn().mockResolvedValue('lmao');
+    spy.mockImplementation(() => {
+        throw new Error('my error message');
+    });
+    spy.mockImplementation(() => {
+        throw new Error('awesome error');
+    });
+    spy.mockImplementation(() => {
+        throw 42;
+    });
     `
   )