Skip to content

Commit 4798005

Browse files
authored
fix(valid-expect-in-promise): support additional test functions (#915)
1 parent 9c89855 commit 4798005

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/rules/__tests__/valid-expect-in-promise.test.ts

+49-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ ruleTester.run('valid-expect-in-promise', rule, {
5151
});
5252
});
5353
`,
54+
dedent`
55+
xtest('it1', function() {
56+
return somePromise.catch(function() {
57+
expect(someThing).toEqual(true);
58+
});
59+
});
60+
`,
5461
dedent`
5562
it('it1', function() {
5663
return somePromise.then(function() {
@@ -152,7 +159,16 @@ ruleTester.run('valid-expect-in-promise', rule, {
152159
const promise = something().then(value => {
153160
expect(value).toBe('red');
154161
});
155-
162+
163+
return promise;
164+
});
165+
`,
166+
dedent`
167+
test.only('later return', () => {
168+
const promise = something().then(value => {
169+
expect(value).toBe('red');
170+
});
171+
156172
return promise;
157173
});
158174
`,
@@ -300,6 +316,16 @@ ruleTester.run('valid-expect-in-promise', rule, {
300316
`,
301317
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
302318
},
319+
{
320+
code: dedent`
321+
xtest('it1', function() {
322+
somePromise.catch(function() {
323+
expect(someThing).toEqual(true)
324+
})
325+
})
326+
`,
327+
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
328+
},
303329
{
304330
code: dedent`
305331
it('it1', function() {
@@ -374,5 +400,27 @@ ruleTester.run('valid-expect-in-promise', rule, {
374400
`,
375401
errors: [{ column: 9, endColumn: 5, messageId: 'returnPromise' }],
376402
},
403+
{
404+
code: dedent`
405+
fit('it1', () => {
406+
somePromise.then(() => {
407+
doSomeOperation();
408+
expect(someThing).toEqual(true);
409+
})
410+
});
411+
`,
412+
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
413+
},
414+
{
415+
code: dedent`
416+
it.skip('it1', () => {
417+
somePromise.then(() => {
418+
doSomeOperation();
419+
expect(someThing).toEqual(true);
420+
})
421+
});
422+
`,
423+
errors: [{ column: 3, endColumn: 5, messageId: 'returnPromise' }],
424+
},
377425
],
378426
});

src/rules/valid-expect-in-promise.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
import {
77
FunctionExpression,
88
KnownCallExpression,
9-
TestCaseName,
109
createRule,
1110
getAccessorValue,
1211
isExpectMember,
1312
isFunction,
1413
isSupportedAccessor,
14+
isTestCaseCall,
1515
} from './utils';
1616

1717
type MessageIds = 'returnPromise';
@@ -95,16 +95,13 @@ const isPromiseReturnedLater = (
9595
);
9696
};
9797

98-
const isTestFunc = (node: TSESTree.Node) =>
99-
node.type === AST_NODE_TYPES.CallExpression &&
100-
isSupportedAccessor(node.callee) &&
101-
([TestCaseName.it, TestCaseName.test] as string[]).includes(
102-
getAccessorValue(node.callee),
103-
);
104-
10598
const findTestFunction = (node: TSESTree.Node | undefined) => {
10699
while (node) {
107-
if (isFunction(node) && node.parent && isTestFunc(node.parent)) {
100+
if (
101+
isFunction(node) &&
102+
node.parent?.type === AST_NODE_TYPES.CallExpression &&
103+
isTestCaseCall(node.parent)
104+
) {
108105
return node;
109106
}
110107

0 commit comments

Comments
 (0)