Skip to content

Commit 5e3d687

Browse files
authored
fix(prefer-ending-with-an-expect): don't report on await expect (#1752)
1 parent 2635b76 commit 5e3d687

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/rules/__tests__/prefer-ending-with-an-expect.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ruleTester.run('prefer-ending-with-an-expect', rule, {
5353
code: dedent`
5454
test('verifies chained expect method call', () => {
5555
doSomething();
56-
56+
5757
tester
5858
.foo()
5959
.bar()
@@ -70,6 +70,10 @@ ruleTester.run('prefer-ending-with-an-expect', rule, {
7070
`,
7171
options: [{ assertFunctionNames: ['td.verify'] }],
7272
},
73+
{
74+
code: 'it("should pass", async () => expect(true).toBeDefined())',
75+
parserOptions: { ecmaVersion: 2017 },
76+
},
7377
{
7478
code: 'it("should pass", () => expect(true).toBeDefined())',
7579
options: [
@@ -92,6 +96,34 @@ ruleTester.run('prefer-ending-with-an-expect', rule, {
9296
expect(container.toHTML()).toContain('Hello Bob!');
9397
});
9498
`,
99+
{
100+
code: dedent`
101+
it('is a complete test', async () => {
102+
const container = render(Greeter);
103+
104+
expect(container).toBeDefined();
105+
106+
container.setProp('name', 'Bob');
107+
108+
await expect(container.toHTML()).resolve.toContain('Hello Bob!');
109+
});
110+
`,
111+
parserOptions: { ecmaVersion: 2017 },
112+
},
113+
{
114+
code: dedent`
115+
it('is a complete test', async function () {
116+
const container = render(Greeter);
117+
118+
expect(container).toBeDefined();
119+
120+
container.setProp('name', 'Bob');
121+
122+
await expect(container.toHTML()).resolve.toContain('Hello Bob!');
123+
});
124+
`,
125+
parserOptions: { ecmaVersion: 2017 },
126+
},
95127
{
96128
code: dedent`
97129
describe('GET /user', function () {
@@ -280,6 +312,24 @@ ruleTester.run('prefer-ending-with-an-expect', rule, {
280312
},
281313
],
282314
},
315+
{
316+
code: dedent`
317+
it('is a complete test', async () => {
318+
const container = render(Greeter);
319+
320+
await expect(container).toBeDefined();
321+
322+
await container.setProp('name', 'Bob');
323+
});
324+
`,
325+
parserOptions: { ecmaVersion: 2017 },
326+
errors: [
327+
{
328+
messageId: 'mustEndWithExpect',
329+
type: AST_NODE_TYPES.Identifier,
330+
},
331+
],
332+
},
283333
],
284334
});
285335

src/rules/prefer-ending-with-an-expect.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ export default createRule<
118118
return;
119119
}
120120

121-
const lastStatement = getLastStatement(node.arguments[1]);
121+
let lastStatement = getLastStatement(node.arguments[1]);
122+
123+
if (lastStatement?.type === AST_NODE_TYPES.AwaitExpression) {
124+
lastStatement = lastStatement.argument;
125+
}
122126

123127
if (
124128
lastStatement?.type === AST_NODE_TYPES.CallExpression &&

0 commit comments

Comments
 (0)