From 3491dd713214be9caddc13463515d111f1b96f76 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 3 Oct 2021 15:40:38 +1300 Subject: [PATCH] fix(valid-expect-in-promise): support `resolve` & `reject` methods --- .../__tests__/valid-expect-in-promise.test.ts | 36 +++++++++++++++++++ src/rules/valid-expect-in-promise.ts | 22 ++++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/rules/__tests__/valid-expect-in-promise.test.ts b/src/rules/__tests__/valid-expect-in-promise.test.ts index 549f7c468..3ae64efa5 100644 --- a/src/rules/__tests__/valid-expect-in-promise.test.ts +++ b/src/rules/__tests__/valid-expect-in-promise.test.ts @@ -605,6 +605,42 @@ ruleTester.run('valid-expect-in-promise', rule, { return Promise.all([somePromise]); }); `, + dedent` + test('promise test', async function () { + const somePromise = getPromise().then((data) => { + expect(data).toEqual('foo'); + }); + + return Promise.resolve(somePromise); + }); + `, + dedent` + test('promise test', async function () { + const somePromise = getPromise().then((data) => { + expect(data).toEqual('foo'); + }); + + return Promise.reject(somePromise); + }); + `, + dedent` + test('promise test', async function () { + const somePromise = getPromise().then((data) => { + expect(data).toEqual('foo'); + }); + + await Promise.resolve(somePromise); + }); + `, + dedent` + test('promise test', async function () { + const somePromise = getPromise().then((data) => { + expect(data).toEqual('foo'); + }); + + await Promise.reject(somePromise); + }); + `, ], invalid: [ { diff --git a/src/rules/valid-expect-in-promise.ts b/src/rules/valid-expect-in-promise.ts index 3ea541b92..fa0f345a4 100644 --- a/src/rules/valid-expect-in-promise.ts +++ b/src/rules/valid-expect-in-promise.ts @@ -115,16 +115,26 @@ const isPromiseMethodThatUsesValue = ( if ( node.argument.type === AST_NODE_TYPES.CallExpression && - node.argument.arguments.length > 0 && - getNodeName(node.argument) === 'Promise.all' + node.argument.arguments.length > 0 ) { - const [firstArg] = node.argument.arguments; + const nodeName = getNodeName(node.argument); + + if (nodeName === 'Promise.all') { + const [firstArg] = node.argument.arguments; + + if ( + firstArg.type === AST_NODE_TYPES.ArrayExpression && + firstArg.elements.some(nod => isIdentifier(nod, name)) + ) { + return true; + } + } if ( - firstArg.type === AST_NODE_TYPES.ArrayExpression && - firstArg.elements.some(nod => isIdentifier(nod, name)) + ['Promise.resolve', 'Promise.reject'].includes(nodeName as string) && + node.argument.arguments.length === 1 ) { - return true; + return isIdentifier(node.argument.arguments[0], name); } }