Repository: skovhus/jest-codemods
Pull request #618 (2024-09-14 19:12:36)

fix(jasmine): don't transform already transformed nested jasmine spies again
and use returned expression over a full block with one statement for SpyObj method objects

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/618?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.18%. 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 [(`649787c`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/649787c552a541ebcc5ccae563ec9e367ffcc9d6?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     #618      +/-   ##
==========================================
+ Coverage   92.38%   95.18%   +2.79%     
==========================================
  Files          26       27       +1     
  Lines        1944     2077     +133     
  Branches      405      432      +27     
==========================================
+ Hits         1796     1977     +181     
+ 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/618?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 1f52f12..63346e5 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -766,20 +766,23 @@ export default function jasmineGlobals(fileInfo, api, options) {
                 )
               )
             )
-          : spyObjMethods.properties.map((arg) =>
-              j.objectProperty(
+          : spyObjMethods.properties.map((arg) => {
+              const alreadyTransformed =
+                arg.value.type === 'CallExpression' &&
+                arg.value.callee.type === 'MemberExpression' &&
+                arg.value.callee.object.name === 'jest' &&
+                arg.value.callee.property.name === 'fn'
+
+              return j.objectProperty(
                 j.literal(arg.key.name),
-                j.callExpression(
-                  j.memberExpression(j.identifier('jest'), j.identifier('fn')),
-                  [
-                    j.arrowFunctionExpression(
-                      [],
-                      j.blockStatement([j.returnStatement(arg.value)])
-                    ),
-                  ]
-                )
+                !alreadyTransformed
+                  ? j.callExpression(
+                      j.memberExpression(j.identifier('jest'), j.identifier('fn')),
+                      [j.arrowFunctionExpression([], arg.value)]
+                    )
+                  : arg.value
               )
-            )
+            })
 
       if (spyObjProperties !== undefined) {
         properties.push(


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 58537af..7c6b706 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -384,17 +384,9 @@ describe('createSpyObj', () => {
     `,
       `
     const spyObj = {
-        'a': jest.fn(() => {
-            return 42;
-        }),
-
-        'b': jest.fn(() => {
-            return true;
-        }),
-
-        'c': jest.fn(() => {
-            return of(undefined);
-        })
+        'a': jest.fn(() => 42),
+        'b': jest.fn(() => true),
+        'c': jest.fn(() => of(undefined))
     };
     `
     )
@@ -410,14 +402,8 @@ describe('createSpyObj', () => {
     `,
       `
     const spyObj = {
-        'a': jest.fn(() => {
-            return 42;
-        }),
-
-        'b': jest.fn(() => {
-            return true;
-        }),
-
+        'a': jest.fn(() => 42),
+        'b': jest.fn(() => true),
         'c': null
     };
     `
@@ -468,6 +454,25 @@ describe('createSpyObj', () => {
       { parser: 'ts' }
     )
   })
+
+  test('with methods object that contain nested jasmine Spies', () => {
+    expectTransformation(
+      `
+    const spyObj = jasmine.createSpyObj({
+        a: jasmine.createSpy(),
+        b: jasmine.createSpy().and.returnValue(true),
+        c: of(42)
+    });
+    `,
+      `
+    const spyObj = {
+        'a': jest.fn(),
+        'b': jest.fn(() => true),
+        'c': jest.fn(() => of(42))
+    };
+    `
+    )
+  })
 })
 
 describe('types', () => {