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 additional test functions #915

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
50 changes: 49 additions & 1 deletion src/rules/__tests__/valid-expect-in-promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ ruleTester.run('valid-expect-in-promise', rule, {
});
});
`,
dedent`
xtest('it1', function() {
return somePromise.catch(function() {
expect(someThing).toEqual(true);
});
});
`,
dedent`
it('it1', function() {
return somePromise.then(function() {
Expand Down Expand Up @@ -136,7 +143,16 @@ ruleTester.run('valid-expect-in-promise', rule, {
const promise = something().then(value => {
expect(value).toBe('red');
});


return promise;
});
`,
dedent`
test.only('later return', () => {
const promise = something().then(value => {
expect(value).toBe('red');
});

return promise;
});
`,
Expand Down Expand Up @@ -274,6 +290,16 @@ ruleTester.run('valid-expect-in-promise', rule, {
`,
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
},
{
code: dedent`
xtest('it1', function() {
somePromise.catch(function() {
expect(someThing).toEqual(true)
})
})
`,
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
},
{
code: dedent`
it('it1', function() {
Expand Down Expand Up @@ -337,5 +363,27 @@ ruleTester.run('valid-expect-in-promise', rule, {
`,
errors: [{ column: 9, endColumn: 5, messageId: 'returnPromise' }],
},
{
code: dedent`
fit('it1', () => {
somePromise.then(() => {
doSomeOperation();
expect(someThing).toEqual(true);
})
});
`,
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
},
{
code: dedent`
it.skip('it1', () => {
somePromise.then(() => {
doSomeOperation();
expect(someThing).toEqual(true);
})
});
`,
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
},
],
});
15 changes: 6 additions & 9 deletions src/rules/valid-expect-in-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
CalledKnownMemberExpression,
FunctionExpression,
KnownCallExpression,
TestCaseName,
createRule,
getAccessorValue,
isExpectMember,
isFunction,
isSupportedAccessor,
isTestCaseCall,
} from './utils';

type MessageIds = 'returnPromise';
Expand Down Expand Up @@ -94,16 +94,13 @@ const isPromiseReturnedLater = (
);
};

const isTestFunc = (node: TSESTree.Node) =>
node.type === AST_NODE_TYPES.CallExpression &&
isSupportedAccessor(node.callee) &&
([TestCaseName.it, TestCaseName.test] as string[]).includes(
getAccessorValue(node.callee),
);

const findTestFunction = (node: TSESTree.Node | undefined) => {
while (node) {
if (isFunction(node) && node.parent && isTestFunc(node.parent)) {
if (
isFunction(node) &&
node.parent?.type === AST_NODE_TYPES.CallExpression &&
isTestCaseCall(node.parent)
) {
return node;
}

Expand Down