Skip to content

Commit

Permalink
fix(valid-expect-in-promise): support resolve & reject methods
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 8, 2021
1 parent c49f704 commit 3491dd7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
36 changes: 36 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
22 changes: 16 additions & 6 deletions src/rules/valid-expect-in-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 3491dd7

Please sign in to comment.