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 #1207

Merged
merged 1 commit into from Aug 21, 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
11 changes: 11 additions & 0 deletions src/rules/__tests__/prefer-expect-assertions.test.ts
Expand Up @@ -104,6 +104,17 @@ ruleTester.run('prefer-expect-assertions', rule, {
},
],
invalid: [
{
code: 'it("it1", () => foo())',
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: null,
},
],
},
{
code: 'it("it1", () => {})',
errors: [
Expand Down
26 changes: 14 additions & 12 deletions src/rules/prefer-expect-assertions.ts
Expand Up @@ -52,10 +52,10 @@ type MessageIds =
| 'suggestAddingAssertions'
| 'suggestRemovingExtraArguments';

const suggestions: Array<[MessageIds, string]> = [
['suggestAddingHasAssertions', 'expect.hasAssertions();'],
['suggestAddingAssertions', 'expect.assertions();'],
];
// const suggestions: Array<[MessageIds, string]> = [
// ['suggestAddingHasAssertions', 'expect.hasAssertions();'],
// ['suggestAddingAssertions', 'expect.assertions();'],
// ];
Comment on lines +55 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this commented out instead of deleted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh whoops, cause I forgot about it 😅 will clean it up tomorrow along with a few other comments we've got lying around


export default createRule<[RuleOptions], MessageIds>({
name: __filename,
Expand Down Expand Up @@ -248,14 +248,7 @@ export default createRule<[RuleOptions], MessageIds>({

const [, testFn] = node.arguments;

if (
!isFunction(testFn) ||
testFn.body.type !== AST_NODE_TYPES.BlockStatement
) {
return;
}

if (!shouldCheckFunction(testFn)) {
if (!isFunction(testFn) || !shouldCheckFunction(testFn)) {
return;
}

Expand All @@ -268,6 +261,15 @@ export default createRule<[RuleOptions], MessageIds>({
return;
}

const suggestions: Array<[MessageIds, string]> = [];

if (testFn.body.type === AST_NODE_TYPES.BlockStatement) {
suggestions.push(
['suggestAddingHasAssertions', 'expect.hasAssertions();'],
['suggestAddingAssertions', 'expect.assertions();'],
);
}

context.report({
messageId: 'haveExpectAssertions',
node,
Expand Down