Repository: skovhus/jest-codemods
Pull request #326 (2022-07-08 16:33:16)

Allow sinon codemod to detect 'sinon' package
Addresses #325

codecov[bot]: # [Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/326?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
> Merging [#326](https://codecov.io/gh/skovhus/jest-codemods/pull/326?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) (cf0c04f) into [main](https://codecov.io/gh/skovhus/jest-codemods/commit/b640f7bd64060223b76959ea8f9b62c59ba0c4f8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) (b640f7b) will **not change** coverage.
> The diff coverage is `100.00%`.

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


| [Impacted Files](https://codecov.io/gh/skovhus/jest-codemods/pull/326?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/326/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.50% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/326?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/326?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus). Last update [b640f7b...cf0c04f](https://codecov.io/gh/skovhus/jest-codemods/pull/326?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).


Patch:
diff --git a/package.json b/package.json
index 71edff7..dc87099 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "jest-codemods",
-  "version": "0.29.0",
+  "version": "0.29.1",
   "description": "Codemods for migrating test files to Jest",
   "license": "MIT",
   "repository": "skovhus/jest-codemods",
diff --git a/src/transformers/sinon.ts b/src/transformers/sinon.ts
index b8a6248..54d341a 100644
--- a/src/transformers/sinon.ts
+++ b/src/transformers/sinon.ts
@@ -702,7 +702,8 @@ export default function transformer(fileInfo: FileInfo, api: API, options) {
   const j = api.jscodeshift
   const ast = j(fileInfo.source)
 
-  const sinonExpression = removeDefaultImport(j, ast, 'sinon-sandbox')
+  const sinonExpression =
+    removeDefaultImport(j, ast, 'sinon-sandbox') || removeDefaultImport(j, ast, 'sinon')
 
   if (!sinonExpression) {
     console.warn(`no sinon for "${fileInfo.path}"`)


Test patch:
diff --git a/src/transformers/sinon.test.ts b/src/transformers/sinon.test.ts
index ba64f05..9faed7a 100644
--- a/src/transformers/sinon.test.ts
+++ b/src/transformers/sinon.test.ts
@@ -32,11 +32,15 @@ function expectTransformation(
   })
 }
 
-it('removes imports', () => {
+it.each`
+  source
+  ${'sinon-sandbox'}
+  ${'sinon'}
+`('removes imports', ({ source }) => {
   expectTransformation(
     `
       import foo from 'foo'
-      import sinon from 'sinon-sandbox';
+      import sinon from '${source}';
 `,
     `
       import foo from 'foo'
@@ -48,7 +52,7 @@ describe('spies and stubs', () => {
   it('handles spies', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
 
         const stub = sinon.stub(Api, 'get');
         const putOk = sinon.stub(Api, 'put').returns(200)
@@ -88,7 +92,7 @@ describe('spies and stubs', () => {
   it('handles 3rd argument implementation fn', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
         sinon.stub(I18n, 'extend', () => 'foo');
 `,
       `
@@ -100,7 +104,7 @@ describe('spies and stubs', () => {
   it('mock clear if spy added in beforeEach', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
 
         beforeEach(() => {
           sinon.stub(Api, 'get')
@@ -125,7 +129,7 @@ describe('spies and stubs', () => {
   it('handles returns', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
         const stub1 = sinon.stub(Api, 'get').returns('foo')
         const stub2 = sinon.stub(Api, 'get').returns(Promise.resolve({ foo: '1' }))
 `,
@@ -139,7 +143,7 @@ describe('spies and stubs', () => {
   it('handles .returnsArg', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
         sinon.stub(foo, 'getParam').returnsArg(3);
   `,
       `
@@ -151,7 +155,7 @@ describe('spies and stubs', () => {
   it('handles .withArgs returns', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
 
         sinon.stub().withArgs('foo').returns('something')
         sinon.stub().withArgs('foo', 'bar').returns('something')
@@ -197,7 +201,7 @@ describe('spies and stubs', () => {
   it('handles .withArgs for typescript files', () => {
     expectTransformation(
       `
-      import sinon from 'sinon-sandbox'
+      import sinon from 'sinon'
 
       sinon.stub().withArgs('foo').returns('something')
     `,
@@ -218,7 +222,7 @@ describe('spies and stubs', () => {
   it('handles .getCall, .getCalls and spy arguments', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
 
         apiStub.getCall(0)
         apiStub.getCall(0).args[1].data
@@ -245,7 +249,7 @@ describe('spies and stubs', () => {
   it('handles .args[n]', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
 
         apiStub.args[2][3]
         apiStub.foo.bar.args[2][3]
@@ -266,7 +270,7 @@ describe('spies and stubs', () => {
   it('handles .nthCall', () => {
     expectTransformation(
       `
-        import sinon from 'sinon-sandbox'
+        import sinon from 'sinon'
 
         apiStub.firstCall
         apiStub.firstCall.args[1].data