skovhus / jest-codemods

Codemods for migrating to Jest https://github.com/facebook/jest 👾
MIT License
881 stars 82 forks source link

chai-should: improve contain transformations #501

Closed kristerkari closed 1 year ago

kristerkari commented 1 year ago

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:

expect(getLink("en")).to.contain(encodeURIComponent("test client name en"));

...gets transformed to:

expect(getLink("en")).toEqual(expect.arrayContaining([encodeURIComponent("test client name en")]));

...and after these changes:

expect(getLink("en")).toContain(encodeURIComponent("test client name en"));

ping @skovhus

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 75.00% and project coverage change: +0.05 :tada:

Comparison is base (87daaa6) 92.39% compared to head (70eb736) 92.45%.

Additional details and impacted files ```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)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.