Repository: skovhus/jest-codemods
Pull request #501 (2023-03-22 10:54:46)

chai-should: improve contain transformations
Hello,

When transforming a large test suite I noticed that the `.to.contain` transformations are not done very well when there are function calls involved. 

In the case when the argument for `expect()` or `.to.contain()` is a function call, it would be better to simply transform `.to.contain()` => `.toContain()` to avoid any problematic results, which is what my changes do.

Since in Jest's `.toContain` works on strings and arrays, it should not be needed to transform `.to.contain` to anything more complicated when we don't know what the result of the function call is.

A real example from the codebase that I'm transforming:

```js
expect(getLink("en")).to.contain(encodeURIComponent("test client name en"));
```
...gets transformed to:

```js
expect(getLink("en")).toEqual(expect.arrayContaining([encodeURIComponent("test client name en")]));
```

...and after these changes:

```js
expect(getLink("en")).toContain(encodeURIComponent("test client name en"));
```

ping @skovhus 



codecov[bot]: ## [Codecov](https://codecov.io/gh/skovhus/jest-codemods/pull/501?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
Patch coverage: **`75.00`**% and project coverage change: **`+0.05`** :tada:
> Comparison is base [(`87daaa6`)](https://codecov.io/gh/skovhus/jest-codemods/commit/87daaa67390d55e38258c9143f0929b346d0cf43?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) 92.39% compared to head [(`70eb736`)](https://codecov.io/gh/skovhus/jest-codemods/pull/501?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) 92.45%.

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


```diff
@@            Coverage Diff             @@
##             main     #501      +/-   ##
==========================================
+ Coverage   92.39%   92.45%   +0.05%     
==========================================
  Files          26       26              
  Lines        1934     1935       +1     
  Branches      404      403       -1     
==========================================
+ Hits         1787     1789       +2     
  Misses        101      101              
+ Partials       46       45       -1     
```


| [Impacted Files](https://codecov.io/gh/skovhus/jest-codemods/pull/501?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) | Coverage Δ | |
|---|---|---|
| [src/transformers/chai-should.ts](https://codecov.io/gh/skovhus/jest-codemods/pull/501?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus#diff-c3JjL3RyYW5zZm9ybWVycy9jaGFpLXNob3VsZC50cw==) | `95.10% <75.00%> (+0.32%)` | :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)

</details>

[:umbrella: View full report in Codecov by Sentry](https://codecov.io/gh/skovhus/jest-codemods/pull/501?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).


Patch:
diff --git a/src/transformers/chai-should.ts b/src/transformers/chai-should.ts
index 60cde8d..a5e9660 100644
--- a/src/transformers/chai-should.ts
+++ b/src/transformers/chai-should.ts
@@ -691,10 +691,13 @@ export default function transformer(fileInfo, api, options) {
 
             // handle `expect(wrapper.text()).to.contain('string')`
             const expectArg = rest.arguments[0]
-            if (
-              expectArg?.type === j.CallExpression.name &&
-              expectArg.callee?.property?.name === 'text'
-            ) {
+            const isExpectArgCallExpression = expectArg.type === j.CallExpression.name
+
+            // handle `expect(a).to.contain(fn('bar'))`
+            const containsArg = args?.[0]
+            const isContainsArgCallExpression = containsArg.type === j.CallExpression.name
+
+            if (isExpectArgCallExpression || isContainsArgCallExpression) {
               return createCall('toContain', args, rest, containsNot)
             }
 


Test patch:
diff --git a/src/transformers/chai-should.test.ts b/src/transformers/chai-should.test.ts
index 4d9c7d9..70e218a 100644
--- a/src/transformers/chai-should.test.ts
+++ b/src/transformers/chai-should.test.ts
@@ -611,16 +611,24 @@ test('converts "includes-contains"', () => {
   expectTransformation(
     `
         expect('foobar').to.contain('foo');
+        expect(fn('foobar')).to.contain('foo');
+        expect(fn('foobar')).to.contain(\`foo\`);
         expect([1, 2, 3]).to.include(2);
         expect('foobar').which.contains('foo');
         expect({ foo: 1, bar: 2 }).to.contain({ bar: 2 });
+        expect(a).to.contain('foo');
+        expect(a).to.contain(fn('bar'));
         expect(a).to.contain(b);
     `,
     `
         expect('foobar').toContain('foo');
+        expect(fn('foobar')).toContain('foo');
+        expect(fn('foobar')).toContain(\`foo\`);
         expect([1, 2, 3]).toContain(2);
         expect('foobar').toContain('foo');
         expect({ foo: 1, bar: 2 }).toMatchObject({ bar: 2 });
+        expect(a).toContain('foo');
+        expect(a).toContain(fn('bar'));
         expect(a).toEqual(expect.arrayContaining([b]));
     `
   )