Repository: skovhus/jest-codemods
Pull request #630 (2025-01-11 18:41:32)

feat(jasmine-globals): add support for expectAsync with matchers toBeResolvedTo, toBeResolved, toBeRejected, toBeRejectedWith, toBeRejectedWithError
add support for [expectAsync](https://jasmine.github.io/api/edge/global.html#expectAsync) with some of its matchers

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/skovhus/jest-codemods/pull/630?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) Report
Attention: Patch coverage is `94.44444%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 92.92%. Comparing base [(`a94e2b5`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/a94e2b561fed619cbebf0b9c28e0a919c44aa4e3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) to head [(`eff6ad9`)](https://app.codecov.io/gh/skovhus/jest-codemods/commit/eff6ad988784850363d3d481b58b50d4bc2d3e48?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).
> Report is 89 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/skovhus/jest-codemods/pull/630?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) | Patch % | Lines |
|---|---|---|
| [src/transformers/jasmine-globals.ts](https://app.codecov.io/gh/skovhus/jest-codemods/pull/630?src=pr&el=tree&filepath=src%2Ftransformers%2Fjasmine-globals.ts&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus#diff-c3JjL3RyYW5zZm9ybWVycy9qYXNtaW5lLWdsb2JhbHMudHM=) | 94.44% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/skovhus/jest-codemods/pull/630?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus) |

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


```diff
@@            Coverage Diff             @@
##             main     #630      +/-   ##
==========================================
+ Coverage   92.38%   92.92%   +0.53%     
==========================================
  Files          26       27       +1     
  Lines        1944     2148     +204     
  Branches      405      450      +45     
==========================================
+ Hits         1796     1996     +200     
- Misses        102      103       +1     
- Partials       46       49       +3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/skovhus/jest-codemods/pull/630?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Kenneth+Skovhus).   
:loudspeaker: Have feedback on the report? [Share it here](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/jasmine-globals.ts b/src/transformers/jasmine-globals.ts
index f087ed7..024987b 100644
--- a/src/transformers/jasmine-globals.ts
+++ b/src/transformers/jasmine-globals.ts
@@ -140,6 +140,48 @@ export default function jasmineGlobals(fileInfo, api, options) {
       }
     })
 
+  root
+    .find(j.MemberExpression, {
+      object: {
+        type: 'CallExpression',
+        callee: { type: 'Identifier', name: 'expectAsync' },
+      },
+      property: { type: 'Identifier' },
+    })
+    .forEach((path) => {
+      const parentNode = path.parent.node
+      const matcher = path.node.property
+
+      parentNode.callee.object.callee.name = 'expect'
+
+      switch (matcher.name) {
+        case 'toBeResolvedTo': {
+          const argument = parentNode.arguments[0]
+
+          parentNode.callee.property.name = argument
+            ? 'resolves.toBe'
+            : 'resolves.toBeUndefined'
+          break
+        }
+        case 'toBeResolved': {
+          parentNode.callee.property.name = 'resolves.toBeUndefined'
+          break
+        }
+        case 'toBeRejected': {
+          parentNode.callee.property.name = 'rejects.toBeDefined'
+          break
+        }
+        case 'toBeRejectedWith': {
+          parentNode.callee.property.name = 'rejects.toEqual'
+          break
+        }
+        case 'toBeRejectedWithError': {
+          parentNode.callee.property.name = 'rejects.toThrow'
+          break
+        }
+      }
+    })
+
   root
     .find(j.CallExpression, {
       // find `jasmine.createSpy(*).and.*()` expressions


Test patch:
diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts
index 6039350..d502008 100644
--- a/src/transformers/jasmine-globals.test.ts
+++ b/src/transformers/jasmine-globals.test.ts
@@ -124,6 +124,39 @@ describe('jasmine matchers', () => {
   })
 })
 
+describe('expectAsync matchers', () => {
+  test('toBeResolved', () => {
+    expectTransformation(
+      'await expectAsync(promise).toBeResolved()',
+      'await expect(promise).resolves.toBeUndefined()'
+    )
+  })
+
+  test('toBeResolvedTo', () =>
+    expectTransformation(
+      'await expectAsync(Promise.resolve(42)).toBeResolvedTo(42)',
+      'await expect(Promise.resolve(42)).resolves.toBe(42)'
+    ))
+
+  test('toBeRejected', () =>
+    expectTransformation(
+      'await expectAsync(Promise.reject()).toBeRejected()',
+      'await expect(Promise.reject()).rejects.toBeDefined()'
+    ))
+
+  test('toBeRejectedWith', () =>
+    expectTransformation(
+      'await expectAsync(Promise.reject({error: 42})).toBeRejectedWith({error: 42})',
+      'await expect(Promise.reject({error: 42})).rejects.toEqual({error: 42})'
+    ))
+
+  test('toBeRejectedWithError', () =>
+    expectTransformation(
+      `await expectAsync(rejectingPromise).toBeRejectedWithError(new Error('mayday'))`,
+      `await expect(rejectingPromise).rejects.toThrow(new Error('mayday'))`
+    ))
+})
+
 test('spyOn', () => {
   expectTransformation(
     `