Repository: skovhus/jest-codemods
Pull request #380 (2022-09-10 09:33:21)

Fix unexpected transform by sinon transformer
This is for passing below test.

```js
#  src/transformers/sinon.test.ts

it('Test', () => {
  expectTransformation(
    `
      import sinon from 'sinon'
      const a = '1'.toString()
    `,
    `
      const a = '1'.toString()
    `
  )
})
```

[`name in SINON_MOCK_RESETS` in the original code](https://github.com/skovhus/jest-codemods/blob/main/src/transformers/sinon.ts#L718) becomes `true` even if the `name` is `'toString'`. ([Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in#description ))

So the following happens:

```js
// From
import sinon from 'sinon'
const a = '1'.toString()

// To
const a = '1'.function toString() { [native code] }()
```


codecov[bot]: # [Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/380?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
Base: **92.39**% // Head: **92.39**% // No change to project coverage :thumbsup:
> Coverage data is based on head [(`6c5303a`)](https://codecov.io/gh/skovhus/jest-codemods/pull/380?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) compared to base [(`176c5e5`)](https://codecov.io/gh/skovhus/jest-codemods/commit/176c5e5d551e1c3ff15ac597126b7d882fe2711e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).
> Patch coverage: 100.00% of modified lines in pull request are covered.

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


```diff
@@           Coverage Diff           @@
##             main     #380   +/-   ##
=======================================
  Coverage   92.39%   92.39%           
=======================================
  Files          26       26           
  Lines        1933     1933           
  Branches      404      404           
=======================================
  Hits         1786     1786           
  Misses        101      101           
  Partials       46       46           
```


| [Impacted Files](https://codecov.io/gh/skovhus/jest-codemods/pull/380?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/380/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus#diff-c3JjL3RyYW5zZm9ybWVycy9zaW5vbi50cw==) | `90.51% <100.00%> (ø)` | |

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)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/380?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](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).

skovhus: Thanks. Do you mind adding a test case?
FLT4613: Sure, I added it.

Patch:
diff --git a/src/transformers/sinon.ts b/src/transformers/sinon.ts
index d9d9328..a1822a9 100644
--- a/src/transformers/sinon.ts
+++ b/src/transformers/sinon.ts
@@ -716,7 +716,7 @@ function transformMockResets(j, ast) {
         type: 'MemberExpression',
         property: {
           type: 'Identifier',
-          name: (name) => name in SINON_MOCK_RESETS,
+          name: (name) => Object.hasOwn(SINON_MOCK_RESETS, name),
         },
       },
     })


Test patch:
diff --git a/src/transformers/sinon.test.ts b/src/transformers/sinon.test.ts
index 70aef4b..eb0c110 100644
--- a/src/transformers/sinon.test.ts
+++ b/src/transformers/sinon.test.ts
@@ -726,3 +726,19 @@ describe('updates types', () => {
     )
   })
 })
+
+describe('prototype', () => {
+  it('toString', () => {
+    expectTransformation(
+      `
+        import sinon from 'sinon'
+        const x = 1
+        const y = x.toString()
+      `,
+      `
+        const x = 1
+        const y = x.toString()
+      `
+    )
+  })
+})