Skip to content

Commit

Permalink
fix(valid-expect-in-promise): support additional test functions (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Sep 28, 2021
1 parent 9c89855 commit 4798005
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
50 changes: 49 additions & 1 deletion src/rules/__tests__/valid-expect-in-promise.test.ts
Expand Up @@ -51,6 +51,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 @@ -152,7 +159,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 @@ -300,6 +316,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 @@ -374,5 +400,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
Expand Up @@ -6,12 +6,12 @@ import {
import {
FunctionExpression,
KnownCallExpression,
TestCaseName,
createRule,
getAccessorValue,
isExpectMember,
isFunction,
isSupportedAccessor,
isTestCaseCall,
} from './utils';

type MessageIds = 'returnPromise';
Expand Down Expand Up @@ -95,16 +95,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

0 comments on commit 4798005

Please sign in to comment.