Skip to content

Commit

Permalink
fix(valid-expect-in-promise): support finally (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Sep 28, 2021
1 parent ec432c1 commit 9c89855
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
37 changes: 37 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Expand Up @@ -21,13 +21,29 @@ ruleTester.run('valid-expect-in-promise', rule, {
});
}));
`,
dedent`
it('it1', () => new Promise((done) => {
test()
.finally(() => {
expect(someThing).toEqual(true);
done();
});
}));
`,
dedent`
it('it1', () => {
return somePromise.then(() => {
expect(someThing).toEqual(true);
});
});
`,
dedent`
it('it1', () => {
return somePromise.finally(() => {
expect(someThing).toEqual(true);
});
});
`,
dedent`
it('it1', function() {
return somePromise.catch(function() {
Expand Down Expand Up @@ -234,6 +250,16 @@ ruleTester.run('valid-expect-in-promise', rule, {
`,
errors: [{ column: 3, endColumn: 6, messageId: 'returnPromise' }],
},
{
code: dedent`
it('it1', () => {
somePromise.finally(() => {
expect(someThing).toEqual(true);
});
});
`,
errors: [{ column: 3, endColumn: 6, messageId: 'returnPromise' }],
},
// {
// code: `
// it('it1', () => {
Expand Down Expand Up @@ -326,6 +352,17 @@ ruleTester.run('valid-expect-in-promise', rule, {
`,
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
},
{
code: dedent`
it('it1', () => {
somePromise.finally(() => {
doSomeOperation();
expect(someThing).toEqual(true);
})
});
`,
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
},
{
code: dedent`
test('invalid return', () => {
Expand Down
15 changes: 8 additions & 7 deletions src/rules/valid-expect-in-promise.ts
Expand Up @@ -4,7 +4,6 @@ import {
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
CalledKnownMemberExpression,
FunctionExpression,
KnownCallExpression,
TestCaseName,
Expand All @@ -18,15 +17,17 @@ import {
type MessageIds = 'returnPromise';
type RuleContext = TSESLint.RuleContext<MessageIds, unknown[]>;

type ThenOrCatchCallExpression = KnownCallExpression<'then' | 'catch'>;
type PromiseChainCallExpression = KnownCallExpression<
'then' | 'catch' | 'finally'
>;

const isThenOrCatchCall = (
const isPromiseChainCall = (
node: TSESTree.Node,
): node is ThenOrCatchCallExpression =>
): node is PromiseChainCallExpression =>
node.type === AST_NODE_TYPES.CallExpression &&
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property) &&
['then', 'catch'].includes(getAccessorValue(node.callee.property));
['then', 'catch', 'finally'].includes(getAccessorValue(node.callee.property));

const isExpectCallPresentInFunction = (body: TSESTree.Node) => {
if (body.type === AST_NODE_TYPES.BlockStatement) {
Expand Down Expand Up @@ -122,7 +123,7 @@ const isParentThenOrPromiseReturned = (

const verifyExpectWithReturn = (
promiseCallbacks: Array<TSESTree.CallExpressionArgument | undefined>,
node: CalledKnownMemberExpression<'then' | 'catch'>,
node: PromiseChainCallExpression['callee'],
context: RuleContext,
testFunctionBody: TSESTree.Statement[],
) => {
Expand Down Expand Up @@ -167,7 +168,7 @@ export default createRule<unknown[], MessageIds>({
return {
CallExpression(node) {
if (
!isThenOrCatchCall(node) ||
!isPromiseChainCall(node) ||
(node.parent && node.parent.type === AST_NODE_TYPES.AwaitExpression)
) {
return;
Expand Down

0 comments on commit 9c89855

Please sign in to comment.