diff --git a/docs/rules/await-async-query.md b/docs/rules/await-async-query.md index 29f32cac..0f86c9eb 100644 --- a/docs/rules/await-async-query.md +++ b/docs/rules/await-async-query.md @@ -52,6 +52,10 @@ const bar = () => { // return the promise within a function is correct too! const findMyButton = () => findByText('my button'); + +// using a resolves/rejects matcher is also correct +expect(findByTestId('alert')).resolves.toBe('Success'); +expect(findByTestId('alert')).rejects.toBe('Error'); ``` ## Further Reading diff --git a/lib/rules/await-async-query.js b/lib/rules/await-async-query.js index 11e10b85..d46ef702 100644 --- a/lib/rules/await-async-query.js +++ b/lib/rules/await-async-query.js @@ -30,7 +30,11 @@ module.exports = { const testingLibraryQueryUsage = []; return { [`CallExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) { - if (!isAwaited(node.parent.parent) && !isPromiseResolved(node)) { + if ( + !isAwaited(node.parent.parent) && + !isPromiseResolved(node) && + !hasClosestExpectResolvesRejects(node) + ) { testingLibraryQueryUsage.push(node); } }, @@ -101,3 +105,19 @@ function isPromiseResolved(node) { // promise.then(...) return hasAThenProperty(parent); } + +function hasClosestExpectResolvesRejects(node) { + if (!node.parent) { + return; + } + + if (node.type === 'CallExpression' && node.callee.name === 'expect') { + const expectMatcher = node.parent.property; + return ( + expectMatcher && + (expectMatcher.name === 'resolves' || expectMatcher.name === 'rejects') + ); + } else { + return hasClosestExpectResolvesRejects(node.parent); + } +} diff --git a/tests/lib/rules/await-async-query.js b/tests/lib/rules/await-async-query.js index f4032413..b21d6a8c 100644 --- a/tests/lib/rules/await-async-query.js +++ b/tests/lib/rules/await-async-query.js @@ -118,6 +118,22 @@ ruleTester.run('await-async-query', rule, { } `, }, + + // resolves/rejects matchers are valid + ...ASYNC_QUERIES_COMBINATIONS.map(query => ({ + code: `test(() => { + expect(${query}("foo")).resolves.toBe("bar") + expect(wrappedQuery(${query}("foo"))).resolves.toBe("bar") + }) + `, + })), + ...ASYNC_QUERIES_COMBINATIONS.map(query => ({ + code: `test(() => { + expect(${query}("foo")).rejects.toBe("bar") + expect(wrappedQuery(${query}("foo"))).rejects.toBe("bar") + }) + `, + })), ], invalid: