Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(valid-expect-in-promise): support finally #914

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(valid-expect-in-promise): support finally
  • Loading branch information
G-Rath committed Sep 25, 2021
commit 0afb9bee191d67c1f0ccf319aabcca3b5ea786fd
37 changes: 37 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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