Skip to content

Commit d0a1585

Browse files
thomaslombartBelco90
authored andcommitted
feat: add support for resolves/rejects matchers in async queries (#64)
* feat: add support for resolves/rejects matchers in async queries * fix: check if there is an expect matcher * docs: add example for resolves/reject matcher in async queries
1 parent 22636b1 commit d0a1585

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

docs/rules/await-async-query.md

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const bar = () => {
5252

5353
// return the promise within a function is correct too!
5454
const findMyButton = () => findByText('my button');
55+
56+
// using a resolves/rejects matcher is also correct
57+
expect(findByTestId('alert')).resolves.toBe('Success');
58+
expect(findByTestId('alert')).rejects.toBe('Error');
5559
```
5660

5761
## Further Reading

lib/rules/await-async-query.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ module.exports = {
3030
const testingLibraryQueryUsage = [];
3131
return {
3232
[`CallExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
33-
if (!isAwaited(node.parent.parent) && !isPromiseResolved(node)) {
33+
if (
34+
!isAwaited(node.parent.parent) &&
35+
!isPromiseResolved(node) &&
36+
!hasClosestExpectResolvesRejects(node)
37+
) {
3438
testingLibraryQueryUsage.push(node);
3539
}
3640
},
@@ -101,3 +105,19 @@ function isPromiseResolved(node) {
101105
// promise.then(...)
102106
return hasAThenProperty(parent);
103107
}
108+
109+
function hasClosestExpectResolvesRejects(node) {
110+
if (!node.parent) {
111+
return;
112+
}
113+
114+
if (node.type === 'CallExpression' && node.callee.name === 'expect') {
115+
const expectMatcher = node.parent.property;
116+
return (
117+
expectMatcher &&
118+
(expectMatcher.name === 'resolves' || expectMatcher.name === 'rejects')
119+
);
120+
} else {
121+
return hasClosestExpectResolvesRejects(node.parent);
122+
}
123+
}

tests/lib/rules/await-async-query.js

+16
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ ruleTester.run('await-async-query', rule, {
118118
}
119119
`,
120120
},
121+
122+
// resolves/rejects matchers are valid
123+
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
124+
code: `test(() => {
125+
expect(${query}("foo")).resolves.toBe("bar")
126+
expect(wrappedQuery(${query}("foo"))).resolves.toBe("bar")
127+
})
128+
`,
129+
})),
130+
...ASYNC_QUERIES_COMBINATIONS.map(query => ({
131+
code: `test(() => {
132+
expect(${query}("foo")).rejects.toBe("bar")
133+
expect(wrappedQuery(${query}("foo"))).rejects.toBe("bar")
134+
})
135+
`,
136+
})),
121137
],
122138

123139
invalid:

0 commit comments

Comments
 (0)