Repository: skovhus/jest-codemods
Pull request #608 (2024-08-29 21:25:08)

fix(jasmine): createSpyObj calls with missing (optional) baseName argument

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

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


```diff
@@            Coverage Diff             @@
##             main     #608      +/-   ##
==========================================
+ Coverage   92.38%   92.67%   +0.28%     
==========================================
  Files          26       27       +1     
  Lines        1944     2047     +103     
  Branches      405      419      +14     
==========================================
+ Hits         1796     1897     +101     
  Misses        102      102              
- Partials       46       48       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/608?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 78b0db8..97f5fbb 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -614,13 +614,21 @@ export default function jasmineGlobals(fileInfo, api, options) {
     })
     .filter((path) => {
       const args = path.node.arguments
+      const isArrayOrObjectExpression = (arg) =>
+        arg.type === 'ArrayExpression' || arg.type === 'ObjectExpression'
+
+      const firstArgumentIsMethodNames = isArrayOrObjectExpression(args[0])
+      if (firstArgumentIsMethodNames) {
+        // ensure that optional baseName is always filled, to simplify arguments handling
+        args.unshift(j.literal(''))
+      }
+
+      const [, spyObjMethods, spyObjProperties] = args
 
       return (
         (args.length === 2 || args.length === 3) &&
-        (args[1].type === 'ArrayExpression' || args[1].type === 'ObjectExpression') &&
-        (args[2] === undefined ||
-          args[2].type === 'ArrayExpression' ||
-          args[2].type === 'ObjectExpression')
+        isArrayOrObjectExpression(spyObjMethods) &&
+        (spyObjProperties === undefined || isArrayOrObjectExpression(spyObjProperties))
       )
     })
     .forEach((path) => {


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 9ae520a..d84e692 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -355,6 +355,33 @@ describe('createSpyObj', () => {
     `
     )
   })
+
+  test('without base name and methods names only', () => {
+    expectTransformation(
+      `
+    const spyObj = jasmine.createSpyObj(['someMethod']);
+    `,
+      `
+    const spyObj = {
+        'someMethod': jest.fn()
+    };
+    `
+    )
+  })
+
+  test('without base name and methods names as well as properties object', () => {
+    expectTransformation(
+      `
+    const spyObj = jasmine.createSpyObj(['someMethod'], {property: true});
+    `,
+      `
+    const spyObj = {
+        'someMethod': jest.fn(),
+        'property': true
+    };
+    `
+    )
+  })
 })
 
 test('return value', () => {