Repository: skovhus/jest-codemods
Pull request #358 (2022-08-19 20:09:40)

[sinon] make returnsArg() TypeScript friendly
Reviewers (in addition to @skovhus): @catc @lencioni @jayrobin

codecov[bot]: # [Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/358?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
> Merging [#358](https://codecov.io/gh/skovhus/jest-codemods/pull/358?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) (17ade00) into [main](https://codecov.io/gh/skovhus/jest-codemods/commit/1a26e298022e7956bc5954cc75816cd6ca58260a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) (1a26e29) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##             main     #358   +/-   ##
=======================================
  Coverage   92.09%   92.09%           
=======================================
  Files          26       26           
  Lines        1872     1873    +1     
  Branches      393      394    +1     
=======================================
+ Hits         1724     1725    +1     
  Misses        102      102           
  Partials       46       46           
```


| [Impacted Files](https://codecov.io/gh/skovhus/jest-codemods/pull/358?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) | Coverage Δ | |
|---|---|---|
| [src/transformers/sinon.ts](https://codecov.io/gh/skovhus/jest-codemods/pull/358/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus#diff-c3JjL3RyYW5zZm9ybWVycy9zaW5vbi50cw==) | `87.55% <100.00%> (+0.05%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus)

danbeam: thanks @jayrobin and @catc! on to @skovhus

Patch:
diff --git a/src/transformers/sinon.ts b/src/transformers/sinon.ts
index 78182ae..67929fe 100644
--- a/src/transformers/sinon.ts
+++ b/src/transformers/sinon.ts
@@ -465,6 +465,8 @@ function transformStubGetCalls(j: core.JSCodeshift, ast) {
     .returnsArg
 */
 function transformMock(j: core.JSCodeshift, ast, parser: string) {
+  const isTypescript = parser === 'tsx' || parser === 'ts'
+
   // stub.withArgs(111).returns('foo') => stub.mockImplementation((...args) => { if (args[0] === '111') return 'foo' })
   ast
     .find(j.CallExpression, {
@@ -529,7 +531,6 @@ function transformMock(j: core.JSCodeshift, ast, parser: string) {
           return j.logicalExpression('&&', logicalExp, binExp)
         })
 
-      const isTypescript = parser === 'tsx' || parser === 'ts'
       const mockImplementationArg = j.spreadPropertyPattern(
         j.identifier.from({
           name: 'args',
@@ -581,10 +582,15 @@ function transformMock(j: core.JSCodeshift, ast, parser: string) {
       node.callee.property.name = 'mockImplementation'
       const argToMock = j.literal(node.arguments[0].value)
 
-      const argsVar = j.identifier('args')
+      const argsVar = j.identifier.from({
+        name: 'args',
+        typeAnnotation: isTypescript
+          ? j.typeAnnotation(j.arrayTypeAnnotation(j.anyTypeAnnotation()))
+          : null,
+      })
       const mockImplementationFn = j.arrowFunctionExpression(
         [j.spreadPropertyPattern(argsVar)],
-        j.memberExpression(argsVar, argToMock)
+        j.memberExpression(j.identifier('args'), argToMock)
       )
       node.arguments = [mockImplementationFn]
       return node


Test patch:
diff --git a/src/transformers/sinon.test.ts b/src/transformers/sinon.test.ts
index fa03982..5b778d9 100644
--- a/src/transformers/sinon.test.ts
+++ b/src/transformers/sinon.test.ts
@@ -152,6 +152,19 @@ describe('spies and stubs', () => {
     )
   })
 
+  it('handles .returnsArg (parser: ts)', () => {
+    expectTransformation(
+      `
+        import sinon from 'sinon'
+        sinon.stub(foo, 'getParam').returnsArg(3);
+  `,
+      `
+        jest.spyOn(foo, 'getParam').mockClear().mockImplementation((...args: any[]) => args[3]);
+  `,
+      { parser: 'ts' }
+    )
+  })
+
   it('handles .withArgs returns', () => {
     expectTransformation(
       `