Skip to content

Commit

Permalink
fix(prefer-expect-assertions): report on concise arrow functions (#1207)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Aug 21, 2022
1 parent 8fb89be commit f928747
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/rules/__tests__/prefer-expect-assertions.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();'],
// ];

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

0 comments on commit f928747

Please sign in to comment.