Repository: skovhus/jest-codemods
Pull request #616 (2024-09-11 18:37:56)

fix(jasmine): type transformation for type jasmine.SpyObj if variable declaration and assignment are separate statements
Otherwise resulted in 

> TypeError: Cannot set properties of undefined (setting 'typeAnnotation')

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

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


```diff
@@            Coverage Diff             @@
##             main     #616      +/-   ##
==========================================
+ Coverage   92.38%   95.15%   +2.76%     
==========================================
  Files          26       27       +1     
  Lines        1944     2064     +120     
  Branches      405      429      +24     
==========================================
+ Hits         1796     1964     +168     
+ 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/616?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 db78300..95130d2 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -673,7 +673,7 @@ export default function jasmineGlobals(fileInfo, api, options) {
         )
       }
 
-      if (typeParameters) {
+      if (typeParameters && path.parentPath.node.id) {
         path.parentPath.node.id.typeAnnotation = j.tsTypeAnnotation(
           j.tsTypeReference(
             j.tsQualifiedName(j.identifier('jest'), j.identifier('Mocked')),


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 9b6c6b4..84c419e 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -427,6 +427,8 @@ describe('types', () => {
       let loggerSpy: jasmine.SpyObj<LoggerService>;
       let unknownSpy: jasmine.SpyObj<unknown>;
       const errorHandlerSpy: jasmine.SpyObj<ErrorHandler> = jasmine.createSpyObj('ErrorHandler', ['handleError']);
+      let translateSpy: jasmine.SpyObj<TranslateService>;
+      translateSpy = jasmine.createSpyObj<TranslateService>(['translate']);
       `,
       `
       let loggerSpy: jest.Mocked<LoggerService>;
@@ -434,6 +436,10 @@ describe('types', () => {
       const errorHandlerSpy: jest.Mocked<ErrorHandler> = {
             'handleError': jest.fn()
       };
+      let translateSpy: jest.Mocked<TranslateService>;
+      translateSpy = {
+            'translate': jest.fn()
+      };
       `,
       { parser: 'ts' }
     ))