Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(await-async-query,await-async-utils,await-fire-event): support handling promises with jest-extended's .toResolve & .toRejects #612

Merged
merged 9 commits into from
Oct 20, 2022
7 changes: 7 additions & 0 deletions docs/rules/await-async-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ problems in the tests. The promise will be considered as handled when:
- wrapped within `Promise.all` or `Promise.allSettled` methods
- chaining the `then` method
- chaining `resolves` or `rejects` from jest
- chaining `toResolve()` or `toReject()` from [jest-extended](https://github.com/jest-community/jest-extended#promise)
- it's returned from a function (in this case, that particular function will be analyzed by this rule too)

Examples of **incorrect** code for this rule:
Expand Down Expand Up @@ -90,6 +91,12 @@ expect(findByTestId('alert')).resolves.toBe('Success');
expect(findByTestId('alert')).rejects.toBe('Error');
```

```js
// using a toResolve/toReject matcher is also correct
expect(findByTestId('alert')).toResolve();
expect(findByTestId('alert')).toReject();
```

```js
// sync queries don't need to handle any promise
const element = getByRole('role');
Expand Down
13 changes: 11 additions & 2 deletions lib/node-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export function isPromisesArrayResolved(node: TSESTree.Node): boolean {
* - it's chained with the `then` method
* - it's returned from a function
* - has `resolves` or `rejects` jest methods
* - has `toResolve` or `toReject` jest-extended matchers
*/
export function isPromiseHandled(nodeIdentifier: TSESTree.Identifier): boolean {
const closestCallExpressionNode = findClosestCallExpressionNode(
Expand Down Expand Up @@ -462,9 +463,17 @@ export function getAssertNodeInfo(
return { matcher, isNegated };
}

const matcherNamesHandlePromise = [
'resolves',
'rejects',
'toResolve',
'toReject',
];

/**
* Determines whether a node belongs to an async assertion
* fulfilled by `resolves` or `rejects` properties.
* fulfilled by `resolves` or `rejects` properties or
* by `toResolve` or `toReject` jest-extended matchers
*
*/
export function hasClosestExpectResolvesRejects(node: TSESTree.Node): boolean {
Expand All @@ -478,7 +487,7 @@ export function hasClosestExpectResolvesRejects(node: TSESTree.Node): boolean {
const expectMatcher = node.parent.property;
return (
ASTUtils.isIdentifier(expectMatcher) &&
(expectMatcher.name === 'resolves' || expectMatcher.name === 'rejects')
matcherNamesHandlePromise.includes(expectMatcher.name)
);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/await-async-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ ruleTester.run(RULE_NAME, rule, {
expect(wrappedQuery(${query}("foo"))).resolves.toBe("bar")
`
),
// async queries with toResolve matchers are valid
...createTestCase(
(query) => `
expect(${query}("foo")).toResolve()
expect(wrappedQuery(${query}("foo"))).toResolve()
`
),

// async queries with rejects matchers are valid
...createTestCase(
Expand All @@ -231,6 +238,14 @@ ruleTester.run(RULE_NAME, rule, {
`
),

// async queries with toReject matchers are valid
...createTestCase(
(query) => `
expect(${query}("foo")).toReject()
expect(wrappedQuery(${query}("foo"))).toReject()
`
),

// unresolved async queries with aggressive reporting opted-out are valid
...ALL_ASYNC_COMBINATIONS_TO_TEST.map((query) => ({
settings: { 'testing-library/utils-module': 'test-utils' },
Expand Down