Skip to content

Commit 7b7a396

Browse files
authored
fix(valid-expect): support async expect in ternary statements (#833)
1 parent 1fee973 commit 7b7a396

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

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

+86
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ ruleTester.run('valid-expect', rule, {
7474
return expect(functionReturningAPromise()).resolves.toEqual(1).then(() => expect(Promise.resolve(2)).resolves.toBe(1));
7575
});
7676
`,
77+
dedent`
78+
expect.extend({
79+
toResolve(obj) {
80+
return this.isNot
81+
? expect(obj).toBe(true)
82+
: expect(obj).resolves.not.toThrow();
83+
}
84+
});
85+
`,
86+
dedent`
87+
expect.extend({
88+
toResolve(obj) {
89+
return this.isNot
90+
? expect(obj).resolves.not.toThrow()
91+
: expect(obj).toBe(true);
92+
}
93+
});
94+
`,
95+
dedent`
96+
expect.extend({
97+
toResolve(obj) {
98+
return this.isNot
99+
? expect(obj).toBe(true)
100+
: anotherCondition
101+
? expect(obj).resolves.not.toThrow()
102+
: expect(obj).toBe(false)
103+
}
104+
});
105+
`,
77106
{
78107
code: 'expect(1).toBe(2);',
79108
options: [{ maxArgs: 2 }],
@@ -369,6 +398,63 @@ ruleTester.run('valid-expect', rule, {
369398
],
370399
},
371400

401+
{
402+
code: dedent`
403+
expect.extend({
404+
toResolve(obj) {
405+
this.isNot
406+
? expect(obj).toBe(true)
407+
: expect(obj).resolves.not.toThrow();
408+
}
409+
});
410+
`,
411+
errors: [
412+
{
413+
column: 9,
414+
endColumn: 43,
415+
messageId: 'asyncMustBeAwaited',
416+
},
417+
],
418+
},
419+
{
420+
code: dedent`
421+
expect.extend({
422+
toResolve(obj) {
423+
this.isNot
424+
? expect(obj).resolves.not.toThrow()
425+
: expect(obj).toBe(true);
426+
}
427+
});
428+
`,
429+
errors: [
430+
{
431+
column: 9,
432+
endColumn: 43,
433+
messageId: 'asyncMustBeAwaited',
434+
},
435+
],
436+
},
437+
{
438+
code: dedent`
439+
expect.extend({
440+
toResolve(obj) {
441+
this.isNot
442+
? expect(obj).toBe(true)
443+
: anotherCondition
444+
? expect(obj).resolves.not.toThrow()
445+
: expect(obj).toBe(false)
446+
}
447+
});
448+
`,
449+
errors: [
450+
{
451+
column: 9,
452+
endColumn: 43,
453+
messageId: 'asyncMustBeAwaited',
454+
},
455+
],
456+
},
457+
372458
// expect().resolves
373459
{
374460
code:

src/rules/valid-expect.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,23 @@ const isAcceptableReturnNode = (
8080
node: TSESTree.Node,
8181
allowReturn: boolean,
8282
): node is
83+
| TSESTree.ConditionalExpression
8384
| TSESTree.ArrowFunctionExpression
8485
| TSESTree.AwaitExpression
85-
| TSESTree.ReturnStatement =>
86-
(allowReturn && node.type === AST_NODE_TYPES.ReturnStatement) ||
87-
[
86+
| TSESTree.ReturnStatement => {
87+
if (allowReturn && node.type === AST_NODE_TYPES.ReturnStatement) {
88+
return true;
89+
}
90+
91+
if (node.type === AST_NODE_TYPES.ConditionalExpression && node.parent) {
92+
return isAcceptableReturnNode(node.parent, allowReturn);
93+
}
94+
95+
return [
8896
AST_NODE_TYPES.ArrowFunctionExpression,
8997
AST_NODE_TYPES.AwaitExpression,
9098
].includes(node.type);
99+
};
91100

92101
const isNoAssertionsParentNode = (node: TSESTree.Node): boolean =>
93102
node.type === AST_NODE_TYPES.ExpressionStatement ||

0 commit comments

Comments
 (0)