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(prefer-expect-assertions): report on concise arrow functions with expect call #1225

Merged
merged 1 commit into from Aug 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/rules/__tests__/prefer-expect-assertions.test.ts
Expand Up @@ -115,6 +115,28 @@ ruleTester.run('prefer-expect-assertions', rule, {
},
],
},
{
code: "it('resolves', () => expect(staged()).toBe(true));",
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: null,
},
],
},
{
code: "it('resolves', async () => expect(await staged()).toBe(true));",
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: null,
},
],
},
{
code: 'it("it1", () => {})',
errors: [
Expand Down
11 changes: 6 additions & 5 deletions src/rules/prefer-expect-assertions.ts
Expand Up @@ -16,6 +16,12 @@ const isFirstStatement = (node: TSESTree.CallExpression): boolean => {
return parent.parent.body[0] === parent;
}

// if we've hit an arrow function, then it must have a single expression
// as its body, as otherwise we would have hit the block statement already
if (parent.parent?.type === AST_NODE_TYPES.ArrowFunctionExpression) {
return true;
}

parent = parent.parent;
}

Expand Down Expand Up @@ -52,11 +58,6 @@ type MessageIds =
| 'suggestAddingAssertions'
| 'suggestRemovingExtraArguments';

// const suggestions: Array<[MessageIds, string]> = [
// ['suggestAddingHasAssertions', 'expect.hasAssertions();'],
// ['suggestAddingAssertions', 'expect.assertions();'],
// ];

export default createRule<[RuleOptions], MessageIds>({
name: __filename,
meta: {
Expand Down